0

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

4
Contributors
11
Replies
12
Views
5 Years
Discussion Span
Last Post by manugm_1987
Featured Replies
  • This works [CODE]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; … Read More

  • I, hope this is what you need Read More

0

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.

0

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"

0

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();
0

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.

Edited by manugm_1987: n/a

0

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.

0

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 :)

0

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.

1

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.

Edited by manugm_1987: n/a

0

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

Cheers Sam

0

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;
        }
    }
}

Edited by manugm_1987: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.