CSharp · Console
using System;
using System.Linq;
string[] tests = new[] { "oo", "ooo", "oooo" };
int maxLength = tests.Select(v => v.Length).Max();
foreach (string s in tests)
{
Console.WriteLine($"\"{s}\"".PadRight(maxLength + 2) + $" -> {s.Count("oo")}");
}
public static class Ext
{
public static int Count (this string s, string substr, StringComparison strComp = StringComparison.CurrentCulture)
{
int count = 0, index = s.IndexOf(substr, strComp);
while (index != -1)
{
count++;
index = s.IndexOf(substr, index + substr.Length, strComp);
}
return count;
}
}