Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

how to convert string like "[[2,3],[4,5],[3,4]]" to two dimensional integer array.
Posted
Updated 8-Oct-12 18:40pm
v3
Comments
Swathi Nagaraj 9-Oct-12 0:45am    
Where is the sting? Can you explain with an example
Varun Sareen 9-Oct-12 1:16am    
you cannot convert this string into two dimensional array. Try to put your question clearly here.

What is the issue your are facing?
Narra sreenu 9-Oct-12 6:26am    
I have a two dimensional array in java script like

var exArray = [[2, 3], [3, 4], [5, 6]];
These array values are stored in a hidden variable using json object.
document.getElementById("hdnArray").value = JSON.stringify(exArray);


In Code side, the hidden variable value as "[[2, 3], [3, 4], [5, 6]]".

Now I want to convert these hidden variable value into two dimensional array with out using spliting operation.

Hi Narra Sreenu,

It will Help You.

C#
int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

        String res = "[";
        int row = 4, column = 2;
        for (int i = 0; i < row; i++)
        {
            res = res + "[";
            for (int j = 0; j < column; j++)
            {
                res = res + myArray[i, j] + ",";
            }
            res = res.TrimEnd(',');
            res = res + "],";
        }
        res = res.TrimEnd(',');
        res = res + "]";

Be Happy Be Coding.
 
Share this answer
 
Hi dear ,
use this

C#
var sb = new StringBuilder();
sb.Append("1,1,2,2,0,0,a,b,c,d,1,1,2,2,0,1,e,f,g,h,1,1,2,2,0,1,x,y,x,z");
const int k = 3;
var items = sb.ToString().Split(',');
var len = items.Length;
var columns = ((len - 1) / k) + 1; //or (len/k) if (len%k==0)
string[,] result = new string[columns, k];
int c = -1;
int r = -1;
for (int i = 0; i < len; i++)
{
  string item = items[i];
  c = (c + 1) % columns;
  if (c == 0)
  {
    r++;
  }
  result[c, r] = item;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900