This is not exactly Linq, but you can also make an extension method as shown below. This is probably more efficient than any Linq solution:
public static int CountSubStrings(this string input, string delimiter, bool ignoreCase = false) { int instancesNo = 0; int pos = 0; while((pos = input.IndexOf(delimiter, pos, ignoreCase ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture)) != -1) { pos += delimiter.Length; instancesNo++; } return instancesNo; }
source share