Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
hi all,
 
how to convert string like "[[2,3],[4,5],[3,4]]" to two dimensional integer array.
Posted 8-Oct-12 18:38pm
Edited 8-Oct-12 18:40pm
v3
Comments
Member 9039168 at 9-Oct-12 0:45am
   
Where is the sting? Can you explain with an example
Varun Sareen at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Narra Sreenu,
 
It will Help You.
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi dear ,
use this
 
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;
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,651
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,542


Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100