Friday, October 24, 2014

remove white space and blank line from lines of comma text file




System.IO.File.WriteAllText(@"c:\path.txt", line);



Incidentally, using WriteAllText isn't a good choice in this case. It will overwrite the

file for each line so only the last line will be in the file. Something like this might

be better:



using (StreamWriter fs = new StreamWriter("path.txt"))
{
foreach (string line in File.ReadAllLines("TESTDATA.txt"))
{
string line1;
line1 = line.Replace("\t", "");
line1 = line1.Replace("\r", "");
line1 = line1.Trim();
line1 = line1.Replace(" ", "");
if(line1.Length > 0) fs.WriteLine(line1);
}
}



- Wayne


No comments:

Post a Comment