An alternative, besides the option posted by Virgilio, if you are dealing with a heavy file, is to read the file line by 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 are using lists, is the method List<T>.RemoveAll
:
List<string> linhas = new List<string>();
// Inserindo valores na lista...
linhas.RemoveAll(linha => string.IsNullOrEmpty(linha));
That would be a file you carry,?
– novic
exact.. Virgilio, I charge it, manipulate it into an array!
– hrmg