Friday, October 24, 2014

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




foreach (string line in File.ReadAllLines("C:\\TESTDATA.txt"))
{

string line1;
line1 = line.Replace("\t", "");
line1 = line.Replace("\r", "");
line1 = line.Trim();
line1 = line.Replace(" ", "");
}





That's not quite right. Each operation takes the original string ("line"), does the

Replace operation and assigns it to a different string ("line1"). Each operation replaces

the contents of line1, so the effects of the previous assignments to line1 are lost.

It should be more like this:




foreach (string line in File.ReadAllLines("C:\\TESTDATA.txt"))
{

string line1;
line1 = line.Replace("\t", "");
line1 = line1.Replace("\r", "");
line1 = line1.Trim();
line1 = line1.Replace(" ", "");
}



- Wayne


No comments:

Post a Comment