Alternatively, in addition to the option posted by Virgilio , if you are dealing with a heavy read line by file line, in this process you can skip lines that do not satisfy a certain condition, for example, use String.IsNullOrEmpty
to check if the line is empty:
using System.Collections.Generic;
using System.IO;
//....
public static void Main(string[] args)
{
List<string> linhas = new List<string>();
using (StreamReader sr = new StreamReader("foo.txt"))
{
string linha;
while ((linha = sr.ReadLine()) != null)
{
if (string.IsNullOrEmpty(linha)) // Aqui você pode incluir mais condições com ||
{
continue;
}
linhas.Add(linha);
}
}
// Use "linhas" aqui...
}
A second alternative, if you're using lists , is the method List<T>.RemoveAll
:
List<string> linhas = new List<string>();
// Inserindo valores na lista...
linhas.RemoveAll(linha => string.IsNullOrEmpty(linha));