示例输入:
List<string> input = new List<string>();
input.Add("\nHello \nWorld");
input.Add("My \nname \nis \John");
input.Add("\n\n\n\n Just for fun \n\n");
目标字符串:“\ n”
预期输出:“ \ n \ n \ n \ n只是为了好玩\ n \ n 最多”\ n“,出现次数为 6 “
备注:
答案 0 :(得分:1)
您可以使用:
var MaxIndex = input.Select((r, i) =>
new
{
OccuranceCount = r.Count(c => char.ToUpperInvariant(c) == char.ToUpperInvariant(searchChar)),
Index = i
})
.OrderByDescending(t => t.OccuranceCount)
.FirstOrDefault();
所以对于以下代码:
List<string> input = new List<string>();
input.Add("\nHello \nWorld");
input.Add("My \nname \nis John");
input.Add("\n\n\n\n Just for fun \n\n");
char searchChar = '\n';
var MaxIndex = input.Select((r, i) =>
new
{
OccuranceCount = r.Count(c => char.ToUpperInvariant(c) == char.ToUpperInvariant(searchChar)),
Index = i
})
.OrderByDescending(t => t.OccuranceCount)
.FirstOrDefault();
Console.WriteLine("Line: {0}, Occurance Count: {1}", input[MaxIndex.Index], MaxIndex.OccuranceCount);