我是c#的新手,我不明白为什么这不起作用.我想拆分以前拆分的字符串.
我的代码如下:
int i;
string s;
string[] temp, temp2;
Console.WriteLine("write 'a-a,b-b,c-c,d-d'";
s = Console.ReadLine();
temp = s.Split(',');
for (i = 0; i < temp.Length; i++)
temp2[i] = temp[i].Split('-');
Run Code Online (Sandbox Code Playgroud)
我收到以下错误 Cannot implicitly convert type 'string[]' to 'string
我想结束:
temp = {a-a , b-b , c-c , d-d};
temp2 = {{a,a},{b,b},{c,c},{d,d}};
Run Code Online (Sandbox Code Playgroud)
Ant*_*ram 10
结果string.Split()
是string[]
,您应该在分配时通过正确的用法看到string[] temp
.但是当您分配元素时string[] temp2
,您试图在仅用于存储字符串的插槽中存储字符串数组,因此编译器错误.您的代码可以在下面进行简单的更改.
string[] temp;
string[][] temp2; // array of arrays
string s = "a-a,b-b,c-c,d-d";
temp = s.Split(',');
temp2 = new string[temp.Length][];
for (int i = 0; i < temp.Length; i++)
temp2[i] = temp[i].Split('-');
Run Code Online (Sandbox Code Playgroud)