This is not as simple as it looks to start with - if you could use Linq on a MatchCollection it would be a whole lot easier...
string inp = "I am going to School I am going to School";
Regex words = new Regex(@"\w+");
MatchCollection matches = words.Matches(inp);
Dictionary<string, List<int>> indexesByWord = new Dictionary<string, List<int>>();
foreach (Match m in matches)
{
string word = m.Value;
if (!indexesByWord.ContainsKey(word))
{
indexesByWord.Add(word, new List<int>());
}
indexesByWord[word].Add(m.Index);
}
foreach (var word in indexesByWord)
{
Console.WriteLine(word.Key);
foreach (int index in word.Value)
{
Console.WriteLine(" {0}", index);
}
}
Will output:
I
0
21
am
2
23
going
5
26
to
11
32
School
14
35
This uses a regex to break the string into individual words, then parses the matches, grouping them by word with each word getting a collection of it's indexes.
As I say - if you could use Linq it would be a lot easier!