【问题标题】:Get total number of non-blank lines from text file?从文本文件中获取非空行的总数?
【发布时间】:2014-04-03 19:44:04
【问题描述】:

我正在使用...

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

...查找文件中的总行数。我该怎么做,但忽略所有空行?

【问题讨论】:

    标签: c# readlines


    【解决方案1】:

    您可以将String.IsNullOrWhiteSpace 方法与Count 一起使用:

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

    或者Allchar.IsWhiteSpace的另一种方式:

    File.ReadLines(@"file.txt").Count(line => !line.All(char.IsWhiteSpace));
    

    【讨论】:

    • 就像一个品种一样,line.Trim() != String.Empty 也可以,虽然可能不是最好的选择。
    猜你喜欢
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多