如果改成这样:
string[] Col1=new string( r.ReadLine().Split(new char[]{' '}));
出错:
d:\soya\io_test\form1.cs(190,18): error CS1502: 与“string.String(char*)”最匹配的重载方法具有一些无效参数
d:\soya\io_test\form1.cs(190,30): error CS1503: 参数“1” : 无法从“string[]”转换为“char*”
d:\soya\io_test\form1.cs(190,18): error CS0029: 无法将类型“string”隐式转换为“string[]”
string[] Col1=new string( r.ReadLine().Split(new char[]{' '}));
出错:
d:\soya\io_test\form1.cs(190,18): error CS1502: 与“string.String(char*)”最匹配的重载方法具有一些无效参数
d:\soya\io_test\form1.cs(190,30): error CS1503: 参数“1” : 无法从“string[]”转换为“char*”
d:\soya\io_test\form1.cs(190,18): error CS0029: 无法将类型“string”隐式转换为“string[]”
能够显示出来!
string [] split = null; Console.WriteLine("The delimiters are -{0}-", delimStr);
for (int x = 1; x <= 5; x++) {
split = words.Split(delimiter, x);
Console.WriteLine("\ncount = {0,2} ..............", x);
foreach (string s in split) {
Console.WriteLine("-{0}-", s);
}
private void button1_Click(object sender, System.EventArgs e)
{
StreamReader r = new StreamReader("d:\\soya\\io_test\\databackup.txt",System.Text.Encoding.Default);
richTextBox1.Text=r.ReadToEnd();
string st=r.ReadLine();
string[] Col1= st.Split(' ');
for(int i=0;i<5;i++)
MessageBox.Show(this,Col1[i]);
} 这样就可以了,你试试! 你不要new char[]{' '},因为这样,c#不知道你new出来的内存单元,要绑定到哪个变量。所以提示“未将对象引用设置到对象的实例。”
richTextBox1.Text=r.ReadToEnd();
string[] Col1= richTextBox1.Text.Split(new char[]{' '});
for(int i=0;i<Col1.Length;i++)
MessageBox.Show(this,Col1[i]);
这样也可以。
string[] Col1= st.Split(' ');
未处理的“System.NullReferenceException”类型的异常出现在 io_test.exe 中。
其他信息: 未将对象引用设置到对象的实例。
用这一句就可以!但是我是用richTextBox1来测试而已,不用这个怎么办???
string[] Col1=r.ReadToEnd().Split(new char []{' '});
for(int i=0;i<Col1.Length;i++)
MessageBox.Show(this,Col1[i]);