Or Yagoda
Or Yagoda

Reputation: 55

List of strings to string

I have a list of strings:

List<string[]> myList

I want to convert it to a string separated by ",". I know how to convert List myList but not what i need.. I tried

String.Join(", ", myList.ToArray());

But i won't work for string[]

I tried to search the internet for solutions but could not found one... I know i can do it with foreach but im looking for one line solution, mostly to learn more advanced coding.

Thank you!

Upvotes: 1

Views: 131

Answers (2)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

Use Join twice.

String.Join(", ", myList.Select(arr => "{" + String.Join(", ", arr) + "}"));

As mentioned by @TimSchmelter use this approach when you want to use different delimiters for each group. so you can join inner array by something like , and the outer list by / or any thing you prefer. also you can use braces to make it look better.

BTW if delimiters are same use the approach given by @YuvalItzchakov

Upvotes: 4

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149518

You can use Enumerable.SelectMany to flatten your List<string[]>:

string.Join(", ", myList.SelectMany(x => x));

Upvotes: 13

Related Questions