문제

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