Split a String Into a 2-Dimensional-Array
This function splits a javascript string into a two dimensional array. The string must have two delimiters embedded in the string. The first delimiter is for the first array, and the second delimiter is for the second array.
function stringTo2dArray(string, d1, d2) {
return string.split(d1).map(function(x){return x.split(d2)});
}
Example:
javascript
var string "this,is,the,first,line. This is the second line."
array2d = stringTo2dArray(string, ". ", ",");
The function's first parameter is the delimeter for the first array, which is the period followed by a space, ie: ". "
The function's second parameter is the delimeter for the second array, which is a comma.
Produces this two-dimentional javascript array:
Array ( [0] => Array ( [0] => This [1] => is [2] => the [3] => first [4] => line ) [1] => Array ( [0] => This [1] => is [2] => the [3] => second [4] => line ) )