Hello!
How do I convert a string, formatted as following:
string str="1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16"
to a multidimensional array, so that:
arr[0,0]=1
arr[0,1]=2
arr[0,2]=3
arr[0,3]=4
arr[1,0]=5
and so on.
The string can have different sizes.
Thanks!
Loading

Blocked AccountRank #403 · 6.2K reputationPosted Nov 29, 2012, 9:39 PM EST
try this
string str = "1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16";
String[][] MultiArray = str.Split(';').Select(t => t.Split(' ')).ToArray();
for accessing the value either you have to write like that or through loop.
string val = MultiArray[0][2];
VulpesRank #55 · 29.2K reputationPosted Nov 30, 2012, 12:55 AM EST
However, if (as the question suggests) you then want to move it to a rectangular array of ints, you can do so - less elegantly - as follows:
string str = "1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16";
Notice that, if the string arrays have different lengths (which they don't in the example), then the second dimension of the rectangular array is taken to be the maximum length of the string arrays. This means that 'unfilled' elements in the rectangular array will have their default values of 0.