Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C如何计算文本文件特定部分的总和?_C#_Visual Studio_Text Files - Fatal编程技术网

C# C如何计算文本文件特定部分的总和?

C# C如何计算文本文件特定部分的总和?,c#,visual-studio,text-files,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 Tuesda

我有这样一个文本文件:

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
我想计算有多少状态是OK,有多少状态是NG。 是否可以将lines.count和lines.contains组合在一起或以其他方式组合?
感谢您的帮助和回复。

以下是您的做法。您唯一应该关心的是您的文件格式是否真正稳定。如果您确定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);
    }
}

告诉我们你尝试了什么,遇到了什么困难。我不知道该从什么开始,有什么建议吗?你应该把你的文本文件转换成csv文件,然后你就可以做到这一点。@MarioDS:谢谢兄弟,我完全学会了。如果你已经有可以编写line.Containsstring的代码,那么就把它设为ifline.EndsWithOK++okcount;else++nogoodcount;谢谢你,我的兄弟。是的,这完全是我需要的。感谢您的解决方案和分享