Skip navigation.
Home

Convert lists to comma separated strings

You have a list or even an array of strings. You want to convert it into a single
string with the list items separated by commas.

Once upon a time you would have looped through the array, added the items to the
string with a comma after each one. Then remembered to trim off th comma after the
last item.

Surely there's an easier way. Meet the Join method of String.

Dim theString as String
theString = String.Join(",", theList.ToArray())

Of course if you're dealing with an array you don't need 'ToArray'

Dim theString as String
theString = String.Join(",", theArray)

The separator doesn't have to be a single character, if you like some space between the
items, that will work too.

Dim theString as String
theString = String.Join(", ", theArray)

And the seperator doesn't even have to be a comma, anything will do.

Dim theString as String
theString = String.Join("|", theArray)

Simple.