我有一个foreach
声明,我从一个文本文件中浏览了几行,在那里我修剪并整理了我需要的行。我想要做的是计算一个相同的字符串出现了多少次。我该怎么做呢?
这是我的代码。if
这是我卡住的第二个陈述:
foreach (string line in lines.Where(l => l.Length >= 5))
{
string a = line.Remove(0, 11);
if ((a.Contains(mobName) && a.Contains("dies")))
{
mobDeathCount++;
}
if (a.Contains(mobName) && a.Contains("drops"))
{
string lastpart = a.Substring(a.LastIndexOf("drops"));
string modifiedLastpart = lastpart.Remove(0, 6);
}
下面是一些行的样子:
一袋硬币
西格白兰地
一袋硬币
一袋硬币
凯丝盾
破烂的卷轴
所以我想要做的是数一数有 3 行一袋硬币。但我需要让它成为一切,有一个巨大的下拉列表。所以不能添加所有的时间,会花费太长时间
编辑
private static void Main()
{
int mobDeathCount = 1;
int lootCheckCount = 1;
string[] lines =
System.IO.File.ReadAllLines(@"C:\Users\Michael\Documents\Electronic Arts\Dark Age of Camelot\chat.log");
Console.WriteLine(
"Enter which mob you want to see, remember to include the, for an example; The siog seeker, remember to start with a capital T");
string mobName = Console.ReadLine();
foreach (string line in lines.Where(l => l.Length >= 5))
{
string a = line.Remove(0, 11);
if ((a.Contains(mobName) && a.Contains("dies")))
{
mobDeathCount++;
}
if (a.Contains(mobName) && a.Contains("drops"))
{
string lastpart = a.Substring(a.LastIndexOf("drops"));
string modifiedLastpart = lastpart.Remove(0, 6);
var lineCountDict = modifiedLastpart.GroupBy(x => x).Where(x => x.Count() > 1).ToDictionary(x => x.Key, x => x.Count());
foreach (var val in lineCountDict)
{
Console.WriteLine(val.Key + " - " + val.Value);
}
新线路;
[01:09:55] siog 搜寻者丢下一袋硬币。
[01:09:55] siog 搜寻者丢下 siog 白兰地。
[01:09:55] siog 探索者死了!
[01:09:55] 你获得 3,687,564 经验值。(1,638,917 营地奖励)
[01:10:31] 你施放了一个次级祛魅喷发法术!
[01:10:31] 你击中了 siog 搜寻者,造成 424 (+18) 伤害!
[01:10:31] siog 搜寻者丢下一袋硬币。
[01:10:31] 你捡起 18 块银和 88 块铜。
[01:10:31] siog 探索者死亡