Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into two dimensional array

How do I make the split string into 2 dimensional array in C# Console App?

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

so that the output would be

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

Returned back to original question. Thanks in advance!

like image 644
Asmo Avatar asked Jan 27 '26 10:01

Asmo


1 Answers

// 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];
}
like image 189
Heinzi Avatar answered Jan 29 '26 00:01

Heinzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!