How can I delete the blank rows of a File.txt with C # [closed]

-1

My File.txt has this form and I want to delete the blank rows

10203040   F       B   B   B   B   B   A   A   X   A   C 

10209647   F       C   A   B   B   B   B   B   A   B   A 

10217686   F       B   B   A   A   A   A   B   B   X   B

10224729   M       B   A   A   X   B   C   A   A   A   X 
    
asked by Walerosys 22.11.2017 в 05:54
source

1 answer

1

Good morning, You could use some LinQ to filter them, I'll give you an example

static void RemoveWhiteLines(string strSourcePath,string strDestinePath)
{
  //Lee todas las lineas del fichero
  string[] strAllLines = File.ReadAllLines(strSourcePath);
  //Selecciona las lineas que no sean null o blancas
  string[] strWritedLines = strAllLines.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
  //Guarda las nuevas lineas en el nuevo fichero
  File.WriteAllLines(strDestinePath, strWritedLines);
}

You could simplify it more too

static void RemoveWhiteLines(string strSourcePath,string strDestinePath)
{
  //Lee todas las lineas del fichero
  string[] strAllLines = File.ReadAllLines(strSourcePath);
  //Selecciona las lineas que no sean null o blancas
  //Guarda las nuevas lineas en el nuevo fichero
  File.WriteAllLines(strDestinePath, strAllLines.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray());
}

What the program does is read all the lines of the path that you indicate, delete the blank lines, and save them in the other file that you indicate, but you could return them, or what interests you.

Atte

    
answered by 27.11.2017 / 13:02
source