user1805430
user1805430

Reputation: 107

Split 2d string array

I got a string[ ][ ], for example {{a,b}{c,d}}. How could convert it to string or using split method to display string[ ][ ] properly?

     string[][] result;
     result = test.AnagramsFinder(inputArray); //which returns string[][]
     string value = string.Join(";",result); // this line does not work for me
     Label1.Text = value ;

is only for string[ ], but not string[ ][ ].

Upvotes: 0

Views: 130

Answers (5)

Jacob
Jacob

Reputation: 759

String.Join(";", result.Select(a => String.Join(",", a)).ToArray());

Output:

a,b;c,d

Upvotes: 0

Spontifixus
Spontifixus

Reputation: 6660

To achieve a nice string representation of your array you can use the following code:

Label1.Text = result
    .Select(item => item.Aggregate((left,right) => left + "," + right))
    .Aggregate((left , right) => left + "|" + right);

for a give input array

var input = new[] {new[]{"a", "b"}, new[]{"c", "d"}};

this delivers the result

a,b|c,d

Upvotes: 0

Carlos Landeras
Carlos Landeras

Reputation: 11063

using System.Linq;

string value = string.Join(";",result.Selectmany(x => x);

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273484

For an "a, b, c, d" result:

 string value = string.Join(", ", result.SelectMany(a => a));

and for the "a, b; c, d" option:

string value = string.Join("; ", result.Select(a => string.Join(", ", a))) ;

Upvotes: 3

Oliver Weichhold
Oliver Weichhold

Reputation: 10296

Even though I cannot follow your usecase for this:

using System.Linq;

string[][] result;
result = test.AnagramsFinder(inputArray); //which returns string[][]
string value = string.Join(";",result.SelectMany(x=> x)); 
Label1.Text = value ;

Upvotes: 1

Related Questions