I have a string containing numbers as a 2D matrix. I'm trying to use Split function to split the contents of a string into an array. So, when I do:
String[] subStrs = new String[20];
subStrs = str.Split('\n');
The code above works fine. However, when I try to create a 2D array and try to populate sub-arrays using the same way:
String[,] numbers = new String[20,20];
for (int i = 0; i < subStrs.Length; i++ )
{
numbers[i] = subStrs[i].Split(' '); //Error
}
I get the following compiler error:
Wrong number of indices inside []; expected 2.
If 2D array is really an array of arrays, then why is the statement numbers[i] = subStrs[i].Split(' '); illegal?
PS : I do know that I can use a nested loop instead to populate numbers. I'm just curious why can't I use the above method?
string[,]andstring[][]are two different things.Splitreturns astring[]so it can be added to an array of arrays (i.e.string[][]), but not a 2D arraystring[,]without some more work (i.e. you'd need an inner loop to assign values to the correct cells in astring[,])subStrs?