This code matches all spaces in the input string and outputs their indexes:
const string sentence = "This is a test sentence.";
MatchCollection matches = Regex.Matches(sentence, @"\s");
foreach (Match match in matches)
{
Console.WriteLine("Space at character {0}", match.Index);
}
This code retrieves all space groups as an array:
const string sentence = "This is a test sentence.";
string[] spaceGroups = Regex.Matches(sentence, @"\s+").Cast<Match>().Select(arg => arg.Value).ToArray();
In either case, you can look at the Match
instances' Index
property values to get the location of the space/space group in the string.