Convert an Array Into a Comma-Delimited String in C#

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 one line. Of course, you can delimit your string with any character you want.

public string ToCSV()
{
	string[] ids = {"2343","2344","2345"};
	string idString = String.Join(",",ids);
	return idString;
}
Next Post Previous Post
No Comment
Add Comment
comment url