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;
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post