C# .NET - how to remove the empty line when reading the text file
Asked By chitra ganapathy on 03-Sep-12 01:45 AM
hi, i want to remove the empty line when reading the text file. if it has empty line means my project is not working. i used the following code to read the text file
StreamReader readtxtfile = new StreamReader(filename);
string str = null;
List<string> str1 = new List<string>();
while ((str = readtxtfile.ReadLine()) != null)
{
str1.Add(Convert.ToString(str));
}
i tried .Trim(), but its not working for me.
Rohan Dave replied to chitra ganapathy on 03-Sep-12 02:58 PM
try to modified your code with below.. see the bold highlighted portion i have added
StreamReader readtxtfile = new StreamReader(filename);
string str = null;
List<string> str1 = new List<string>();
while ((str = readtxtfile.ReadLine()) != null)
{
// if line is empty or blank then it will not insert into your List <string>
if (! String.IsNullOrEmpty(str))
{
str1.Add(Convert.ToString(str));
}
}