IndicesOf

       public static IEnumerable<int> IndicesOf(this string searchIn, string searchFor)
        {
            if (string.IsNullOrEmpty(searchFor)) yield break;
            
            int lastLoc = searchIn.IndexOf(searchFor);
            while (lastLoc != -1)
            {
                yield return lastLoc;
                lastLoc = searchIn.IndexOf(searchFor, startIndex: lastLoc + 1);
            }
        }
Example:
       [Test]
        public void TestStringIndicesOf()
        {
            CollectionAssert.AreEquivalent("babbab".IndicesOf("b"),  new[] { 0,2,3,5});
            CollectionAssert.AreEquivalent("babbab".IndicesOf("ba"), new[] { 0, 3 });
            CollectionAssert.AreEquivalent("babbab".IndicesOf("cgesahghts"), Enumerable.Empty<int>());
            CollectionAssert.AreEquivalent("babbab".IndicesOf(string.Empty), Enumerable.Empty<int>());
        }

Description

Gets all the indexes in which a certain substring appears within the string.

Details

Double click on the code to select all.

 

;