C#如何计算textfile的特定部分的总和?

时间:2016-09-27 07:32:56

标签: c# visual-studio text-files

我有一个这样的文本文件:

IPen ID Datetime                                    Status
48      Tuesday, September 27, 2016 14:17:23        OK
48      Tuesday, September 27, 2016 14:17:26        NG
48      Tuesday, September 27, 2016 14:17:30        OK
48      Tuesday, September 27, 2016 14:17:47        NG
48      Tuesday, September 27, 2016 14:17:50        OK
48      Tuesday, September 27, 2016 14:17:53        OK
48      Tuesday, September 27, 2016 14:17:57        OK
48      Tuesday, September 27, 2016 14:18:00        OK
48      Tuesday, September 27, 2016 14:18:03        OK
48      Tuesday, September 27, 2016 14:18:06        NG
48      Tuesday, September 27, 2016 14:18:10        OK
48      Tuesday, September 27, 2016 14:18:13        NG
48      Tuesday, September 27, 2016 14:18:17        NG

我想计算status OK的数量和status NG的数量lines.count。 是否可以将lines.containsNewsTableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain) 组合在一起或以其他方式组合? 感谢您的帮助和回复。

1 个答案:

答案 0 :(得分:6)

这是你如何做到的。您唯一应该关心的是文件格式是如何稳定的。如果你确定OK和NG总是大写并且在行尾,你可以这样做。否则,你应该找到一套稳定的条件。

class Program
{
    static void Main(string[] args)
    {
        var filePath = @"c:\temp\file.txt";
        var lines = File.ReadAllLines(filePath);
        var oks = lines.Count(s => s.EndsWith("OK"));
        var ngs = lines.Count(s => s.EndsWith("NG"));
        Console.WriteLine("OKs: {0}, NGs {1}",oks,ngs);
    }
}