我有一个看起来是这样的文件:C#:字符串分为子,但不包括标签
0 4 5
6 9 9
等,每排三个数字,由制表符分隔。我从文件中的行的数组:
string[] lines = File.ReadAllLines(theFile);
对于每一行,我打电话,其预期的结果是返回一个整数数组的函数。下面是我的代码看起来像截至目前:
public int[] getPoint(string line)
{
int xlength= line.IndexOf(" "); //find location of first white space
int x= Int32.Parse(line.Substring(0, xlength)); //find x-coordinate and turn it from a string to an int
int ylength = line.IndexOf(" ", xlength); //find next white space
int y = Int32.Parse(line.Substring(xlength + 1, ylength)); //y-coordinate starts after first white space and ends before next
int z = Int32.Parse(line.Substring(ylength + 1, line.Length)); //z starts after 2nd white space and goes through end of line
return new int[] { x, y, z }; //return a new point!
}
我的问题是用的IndexOf(字符串)功能。它不识别标签。我怎么写这个,以便我的每个getPoint函数达到它的目的?谢谢。
选项卡由“\ t”表示。我建议使用Split()方法。 –
考虑不要重新创建String.Split()方法或.NET TextFieldParser类。 –
关于阅读CSV文件有几百个问题。请澄清为什么现有的答案不适合你。 –