Basically I'm reading in 50 lines from a text file which has a format somewhat like this for each line:
David Chalmers 34
I've read the text file using ReadAllLines so each line should be a different entry in the array.
I'm trying to take the number from each line and store them into an array of their own. Right now I'm getting the error:
Index and length must refer to a location within the string.
static void getResults(string[] Text)
{
// X = lastIndexOf for ' ' (space)
// x will take position of the last space
// Results = substring
// z will be the length of the string
// z = text.Length
// Substring (x+1,z-x+1)
int lines = 50;
string[] Results = new string[lines];
for (int i = 0; i < Text.Length; i++)
{
int x = Text[i].LastIndexOf(' ');
int z = Text[i].Length;
Results[lines] = Text[i].Substring(x + 1, z - x + 1);
}
}
Any help would be appreciated!
Results[50]will be off the array - presumably you wantResults[i]z - (x + 1)orz - x - 1string.Split()method to do this it would be more efficient and less code also what if you get more than 50 lines.. at least theSplitfunction would still work as long as the file format remains the same..