Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get total number of non-blank lines from text file?

Tags:

c#

readlines

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?

like image 925
Keavon Avatar asked Mar 02 '14 23:03

Keavon


1 Answers

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));
like image 192
Selman Genç Avatar answered Oct 15 '22 17:10

Selman Genç