Вопрос

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?

Это было полезно?

Решение

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));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top