How do I read and edit a .txt file in C#?
c#, file, text-files
Solution
Added some LINQ for fun and profit (room for optimization ;) ):
System.IO.File.WriteAllLines(
"outfilename.txt",
System.IO.File.ReadAllLines("infilename.txt").Select(line =>
"{" +
string.Join(", ",
line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
) + "}"
).ToArray()
);
Problem
For example, I have a txt file that reads: ``` 12 345 45 2342 234 45 2 2 45345 234 546 34 3 45 65 765 12 23 434 34 56 76 5 ``` I want to insert a comma between all the numbers, add a left brace to the begining of each line and a right brace to the end of each line. So after the editing it should read: ``` {12, 345, 45} {2342, 234, 45, 2, 2, 45345} {234, 546, 34, 3, 45, 65, 765} {12, 23, 434, 34, 56, 76, 5} ``` How do I do it?