0

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[ ][ ].

5 Answers 5

3

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))) ;
Sign up to request clarification or add additional context in comments.

6 Comments

Where is the separator?, it receives two arguments
Hi, thanks for posting. Any ideas how could I split those arrays by using ";" between arrays and "," between elements in single array?
Yeah, forgot that. Fixed some typos now.
@user1805430 - as the name of the function indicates, this is Joining, not Splitting strings.
Thanks. btw, do u how to check if a nested list does not contain a string value?
|
1

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 ;

Comments

1
using System.Linq;

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

2 Comments

Hi, thanks for posting. But I don't have definition for Selectmany..Did I miss any reference?
Yeah, you have to use: using System.Linq; Import it at the beginning of the class
0
String.Join(";", result.Select(a => String.Join(",", a)).ToArray());

Output:

a,b;c,d

2 Comments

Thanks for posting. btw, do u how to check if a nested list does not contain a string value?
@user1805430 String.Join(";", result.Where(x => (!x.Contains(StringToAvoid))).Select(a => String.Join(",", a)).ToArray());
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.