基本上我正在从一个文本文件中读取 50 行,该文件的每一行的格式都类似于这样:
David Chalmers 34
我已经使用 ReadAllLines 读取了文本文件,因此每一行都应该是数组中的不同条目。
我试图从每一行中取出数字并将它们存储到它们自己的数组中。现在我收到错误:
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);
}
}
如有任何帮助,我们将不胜感激!
请您参考如下方法:
使用Split,它更容易:
String Test = "David Chalmers 34";
String[] Segments = Test.Split(' ');
Console.WriteLine(Segments[2]);
Console.ReadKey();
当然,您需要为错误的输入添加错误处理。
