The below code is one way to do it, but I'm not sure I'm totally clear on your needs. Below I get the value, and then the begin and end indices of that value. So it would be [11,0,1], [2,3,3], ..., because that is the actual start and end index of the value. What are you getting the indices of? If you are getting the index of the whitespace character after the end of the token, then remove the subtraction by one when calling the Element constructor. Oh, and I used a Struct rather than Class because it's a bunch faster using the stack instead of the heap.
string values = "11 2 3 4 12 23 456 234";
List<Element> elements = GetElements(values);
public static List<Element> GetElements(string input) {
List<Element> elements = new List<Element>();
string value = "";
for (int beginI = 0, i = 0; i < input.Length; i++)
{
if (input[i] == ' ')
{
elements.Add(new Element(value, beginI, i - 1));
value = "";
beginI = i + 1;
continue;
}
else if(i == input.Length - 1)
elements.Add(new Element(value + input[i], beginI, i - 1));
else
value += input[i];
}
return elements;
}
public struct Element
{
public string Data;
public int End;
public int Begin;
public Element (string data, int begin, int end){
Data = data;
Begin = begin;
End = end;
}
}