C# List<string> to string with delimiter

nan picture nan · Aug 26, 2010 · Viewed 614.8k times · Source

Is there a function in C# to quickly convert some collection to string and separate values with delimiter?

For example:

List<string> names --> string names_together = "John, Anna, Monica"

Answer

Quartermeister picture Quartermeister · Aug 26, 2010

You can use String.Join. If you have a List<string> then you can call ToArray first:

List<string> names = new List<string>() { "John", "Anna", "Monica" };
var result = String.Join(", ", names.ToArray());

In .NET 4 you don't need the ToArray anymore, since there is an overload of String.Join that takes an IEnumerable<string>.

Results:


John, Anna, Monica