以下输入文件
输入文件
a 00002098 0 0.75 unable#1 (usually followed by `to') not having the necessary means or skill or know-how; "unable to get to town without a car"; "unable to obtain funds"
a 00002312 0.23 0.43 dorsal#2 abaxial#1 facing away from the axis of an organ or organism; "the abaxial surface of a leaf is the underside or side facing away from the stem"
a 00023655 0 0.5 outside#10 away#3 able#2 (of a baseball pitch) on the far side of home plate from the batter; "the pitch was away (or wide)"; "an outside pitch"
我想要这个文件
输出的以下结果
a,00002098,0,0.75,unable#1
a,00002312,0.23,0.43,dorsal#2
a,00002312,0.23,0.43,abaxial#1
a,00023655,0, 0.5,outside#10
a,00023655,0, 0.5,away#3
a,00023655,0, 0.5,able#2
我编写以下代码来提取上述结果
TextWriter tw = new StreamWriter("D:\\output.txt");
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != null)
{
StreamReader reader = new StreamReader(@"C:\Users\Zia\Desktop\input.txt");
string line;
String lines = "";
while ((line = reader.ReadLine()) != null)
{
String[] str = line.Split('\t');
String[] words = str[3].Split(' ');
for (int k = 0; k < words.Length; k++)
{
for (int i = 0; i < str.Length; i++)
{
if (i + 1 != str.Length)
{
lines = lines + str[i] + ",";
}
else
{
lines = lines + words[k] + "\r\n";
}
}
}
}
tw.Write(lines);
tw.Close();
reader.Close();
}
}
当我更改索引时,此代码会给出以下错误,而不是给出期望的结果。
错误
索引超出了数组的范围。
提前致谢。