Prakash Prakash - 23 days ago 12
C# Question

How To convert 1D array to 2D array in C#

I have:

string snacks="Chocolate,Alwa,Samosa,Channa";


I need this as array like:

snacks= {"Chocolate,Alwa" , "Samosa,Channa"};
in C#.

Could anyone pls Suggest the way to find my snacks

Answer
        string snack2[][]=new string[Math.Round(snacks.Length/2)][2];
        int cnt=0;
        for(int i=0;i<Math.Round(snacks.Length/2);i++)
        {
             for (int j=0;j<2;j++)
             {
                 snacks2[i][j]=snack[cnt++];
             }
        }
Comments