I'm currently trying to split a string into a 2d array. currently I've split it into a 1d array of each line using:

string[] LinesFile = streamReader.ReadToEnd().Split('\n');

And now I'd like to Split it further into a new variable with a 2d array.
how would I go about doing this.

Cheers Sam

Recommended Answers

All 11 Replies

You can treat a string as an array of characters, so you could do something like this:

for (int i = 0; i < LinesFile.Length; i++) {
    for (int j = 0; j < LinesFile[i].Length; j++) {
        Console.WriteLine("The character at [{0},{1}] is {2}", i, j, LinesFile[i][j]);
    }
}

Otherwise you'll have to use CopyAt if you want a 'real' 2D array.

You can treat a string as an array of characters, so you could do something like this:

for (int i = 0; i < LinesFile.Length; i++) {
    for (int j = 0; j < LinesFile[i].Length; j++) {
        Console.WriteLine("The character at [{0},{1}] is {2}", i, j, LinesFile[i][j]);
    }
}

Otherwise you'll have to use CopyAt if you want a 'real' 2D array.

Thanks for replying so quick but I'm not sure in my first post I made it clear what I wanted to do.

I have in LinesFile[0] a string like "fdkjdgh,fdsfds,fdsfdsf,fdsfds"
and I'd like to split it up so LinesFile[0][0] will equal "fdkjdgh"

Then do:

string[] LinesFile = streamReader.ReadToEnd().Split('\n');
for(int i = 0; i < LinesFile.Lenght; i++)
{
     for(int j = 0; j < LinesFile[i].Lenght; j++)
     {
          string[] LinesData = string.Split(',');
          foreach(string item in LinesData)
          {
               Console.WriteLine(item);
          }
     }
}
Console.ReadLine();

Hope this helps

static void Main(string[] args)
        {
           
            string[] s={"hello,how,are,u",
                        "this,is,second,string"};
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = 0; j<=s[i].Length; j++)
                {
                    string[,] s2=new string[20,20];
                    string[] s3 = s[i].Split(',');
                    s2[i,j]=Convert.ToString(s3[j]);
                    Console.WriteLine(s2[i,j]);
                }
            }
            Console.Read();
        }
    }

I have hardcoded the string array. You can convert it to read from stream.

The method above works if each line has the same number of parts, otherwise you'll need to use a jagged array to hold the pieces.

NO, the above code works for any number of pieces, there is no restriction. Ya of course we can also make use of jagged array :)

NO, the above code works for any number of pieces, there is no restriction. Ya of course we can also make use of jagged array :)

No it doesn't. Try putting this as the first string "this,string,is,too,long,for,this,code,to,handle,so,I'll,get,an,index,out,of,bounds,exception,when,it,runs".

It also doesn't even save a single part (array declaration is in the wrong spot). Nor will it handle more than 20 lines.

You also call Convert.ToString on a string.

This works

static void Main(string[] args)
        {
            int length = 0;
            string[,] s2 = new string[20, 200];
            string[] s ={"this,string,is,too,long,for,this,code,to,handle,so,I'll,get,an,index,out,of,bounds,exception,when,it,runs",
                        "this,is,second,string"};
            for (int m = 0; m < s.Length; m++)
            {
                length += s[m].Length;
            }
            for (int i = 0; i <s.Length; i++)
            {
                   
                for (int j = 0; j<=length; j++)
                {
                    string[] s3 = s[i].Split(',');
                    
                    try
                    {
                        s2[i, j] = Convert.ToString(s3[j]);
                        Console.WriteLine(s2[i, j]);
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
            Console.Read();
        }
    }
}

Modify the declaration and size according to scenario, since example i have just hard coded the array size.

Thanks for the help. Also how can I return a whole 2d array rather than returning s2[0,0] for example.

Cheers Sam

can do this way

class Program
    {
        string[,] s2 = new string[20, 200];
        
        static void Main(string[] args)
        {
            int length = 0;
            //string[,] s4 = new string[20, 200];
            string[] s = { "this,is,second,string", 
                           "this,string,is,too,long,for,this,code,to,handle,so,I'll,get,an,index,out,of,bounds,exception,when,it,runs" };
            //for (int m = 0; m < s.Length; m++)
            //{
            //    length += s[m].Length;
            //}
            Program p = new Program();
            //s4 = p.split(s); or
             p.s2=p.split(s);
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = 0; j < s[i].Length; j++)
                {
                    try
                    {
                        Console.WriteLine(p.s2[i, j].ToString());
                    }
                    catch { };

                }

            }
            Console.Read();
        }
         public  string[,] split(string[] s)
        {
            for (int i = 0; i < s.Length; i++)
            {

                for (int j = 0; j <= s[i].Length; j++)
                {
                    string[] s3 = s[i].Split(',');

                    try
                    {
                        s2[i, j] = Convert.ToString(s3[j]);
                        //Console.WriteLine(s2[i, j]);
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
            return s2;
        }
    }
}

I, hope this is what you need

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.