c#如何计算文本文件中特定部分的总和
本文关键字:定部 文件 文本 何计算 计算 | 更新日期: 2023-09-27 18:09:11
我有一个像这样的文本文件:
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.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);
}
}