Convert an array to a comma-delimited string

Using the static String.Join method is a quick way to get a comma-delimited string. (I used to always run the array through a foreach loop tacking on a comma, and then removing the last comma outside the foreach loop--a bit messy). This code shows you how to take an array and convert it into a string delimited by commas in one nline. Of course, you can delimit your string with any character you want.

string[] ids = {"2343","2344","2345"};
string idString = String.Join(",",ids);
Response.Write(idString);

You might also like...

Comments

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan