The snippet shows how to convert list of string values to comma separated string values in quicker way using String.Join method.
String.Join:
String.Join(String separator, IEnumerable<String> values) - Concatenates the elements of members of a collection using specified character between each element.
Input Arguments:
separator - string to use as a separator
values - A collection which contains strings to concatenate
Output :
Returns separator separated collection values
Example:
using System;
using System.Collections.Generic;
namespace DotNetMirror
{
class ListString2CommaSeparatedString
{
static void Main()
{
List<string> listValues = new List<string>();
listValues.Add("D");
listValues.Add("N");
listValues.Add("M");
string commaSeparatedString = string.Join(",", listValues);
Console.WriteLine(commaSeparatedString);
Console.ReadLine();
}
}
}
Output:
D,N,M