Reputation: 3527
Example input:
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");
Targeted string: "\n"
Expected output: "\n\n\n\n Just for fun \n\n has the most "\n" and the number of occurrence is 6"
Notes:
Upvotes: 1
Views: 85
Reputation: 223187
You can use:
var MaxIndex = input.Select((r, i) =>
new
{
OccuranceCount = r.Count(c => char.ToUpperInvariant(c) == char.ToUpperInvariant(searchChar)),
Index = i
})
.OrderByDescending(t => t.OccuranceCount)
.FirstOrDefault();
So for the following code:
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);
Upvotes: 1