9

I am using...

File.ReadLines(@"file.txt").Count();

...to find the total number of lines in the file. How can I do this, but ignore all blank lines?

Keavon
  • 6,837
  • 9
  • 51
  • 79

1 Answers1

15

You can use String.IsNullOrWhiteSpace method with Count:

File.ReadLines(@"file.txt").Count(line => !string.IsNullOrWhiteSpace(line));

Or another way with All and char.IsWhiteSpace:

File.ReadLines(@"file.txt").Count(line => !line.All(char.IsWhiteSpace));
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • just as a variety, also `line.Trim() != String.Empty` would work, though probably not the best option. – Rotem Mar 02 '14 at 23:53