0

Possible Duplicate:
Array.Join in .Net?

From a List<String> that contains "Hello" and "World", I would like to obtain "Hello, World".

Is there a library function that would accept my list and and the string ", " and return "Hello, World" ?

Community
  • 1
  • 1
Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90

3 Answers3

5

Yes, there is: not surprisingly, it is called Join

String.Join(", ", new string[] {"hello", "world"});

In .NET 4.0 and later, the second parameter could be an IEnumerable<T>.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Actually, in .NET 4 they added `params` to [`String.Join`](http://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.100).aspx) so you can do `String.Join(", ", "hello", "world");` without the `new string[] { ... }` part ;) – khellang Nov 01 '12 at 16:55
  • Thanks, I was looking in the List class – Samuel Rossille Nov 01 '12 at 16:59
4

Yes; String.Join(separator, array)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0
string.join(yourlist.ToArray())
John Conde
  • 217,595
  • 99
  • 455
  • 496
albattran
  • 1,887
  • 1
  • 12
  • 16