繁体   English   中英

将字符串拆分为二维数组

[英]Split string into two dimensional array

如何在C#Console App中将拆分字符串转换为二维数组?

char[,] table2x2 = new char[2, 2]; 
string myString = "11A23A4A5A"; 
string[] splitA = myString.Split(new char[] { 'A' });

这样输出就可以了

Console.WriteLine(table3x3[0, 0]); //output: 11
Console.WriteLine(table3x3[0, 1]); //output: 23
Console.WriteLine(table3x3[1, 0]); //output: 4
Console.WriteLine(table3x3[1, 1]); //output: 5

回到原来的问题。 提前致谢!

// your code, char[,] replaced by string[,]
string[,] table2x2 = new string[2, 2];  
string myString = "11A23A4A5A"; 
string[] splitA = myString.Split(new char[] { 'A' }); 

// this converts splitA into a 2D array
// Math.Min is used to avoid filling past the array bounds
for (int i = 0; i < Math.Min(splitA.Length, 2*2); i++) {
    table2x2[i / 2, i % 2] = splitA[i];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM