using System.Collections.Generic;
public static IEnumerable<string> GetSubStrings(string input, List<int> source)
for (var i = 0; i < source.Count; i += 2)
yield return input.Substring(source[i], source[i + 1] - source[i]);
public static List<string> GetSubStrings2(string input, List<int> source)
var result = new List<string>();
for (var i = 0; i < source.Count; i += 2)
result.Add(input.Substring(source[i], source[i + 1] - source[i]));
public static List<int> AllIndexesOf(string str, string value)
if (String.IsNullOrEmpty(value))
throw new ArgumentException("the string to find may not be empty", "value");
List<int> indexes = new List<int>();
for (int index = 0;; index += value.Length)
index = str.IndexOf(value, index);
public static void Main()
var input = "2abcdef2ggg2abcdefgh2";
var indexes = AllIndexesOf(input, "2");
Console.WriteLine(string.Join(", ", GetSubStrings(input, indexes)));
Console.WriteLine(string.Join(", ", GetSubStrings2(input, indexes)));