.indexOf for multiple results

Here I got a extension method for that, for the same use as IndexOf:

public static IEnumerable<int> AllIndexesOf(this string str, string searchstring)
{
    int minIndex = str.IndexOf(searchstring);
    while (minIndex != -1)
    {
        yield return minIndex;
        minIndex = str.IndexOf(searchstring, minIndex + searchstring.Length);
    }
}

so you can use

s.AllIndexesOf(","); // 5    14    27    37

https://dotnetfiddle.net/DZdQ0L

Leave a Comment