88

Is there a fast way to convert List<string> to a comma-separated string in C#?

I do it like this but Maybe there is a faster or more efficient way?

List<string> ls = new List<string>();
ls.Add("one");
ls.Add("two");
string type = string.Join(",", ls.ToArray());

PS: Searched on this site but most solutions are for Java or Python

6
  • 2
    Nope, you're doing it right. Although I don't think you need to do ToArray(). Commented Dec 21, 2011 at 16:41
  • 2
    Faster to write or faster to execute? Commented Dec 21, 2011 at 16:42
  • Both :), But apparently it is not possible Commented Dec 21, 2011 at 16:49
  • @Yuck It is needed to convert to string[] (Because I don't use .NET 4) Commented Dec 21, 2011 at 16:49
  • @Ozkan You should probably tag your question for the framework version you're working against then. I think you'll find most answers will assume you're using the latest release. Commented Dec 21, 2011 at 17:06

6 Answers 6

125

In .NET 4 you don't need the ToArray() call - string.Join is overloaded to accept IEnumerable<T> or just IEnumerable<string>.

There are potentially more efficient ways of doing it before .NET 4, but do you really need them? Is this actually a bottleneck in your code?

You could iterate over the list, work out the final size, allocate a StringBuilder of exactly the right size, then do the join yourself. That would avoid the extra array being built for little reason - but it wouldn't save much time and it would be a lot more code.

Sign up to request clarification or add additional context in comments.

1 Comment

string.Join(",",myList )
20

To expand on Jon Skeets answer the code for this in .Net 4 is:

string myCommaSeperatedString = string.Join(",",ls);

Comments

15

The following will result in a comma separated list. Be sure to include a using statement for System.Linq

List<string> ls = new List<string>();
ls.Add("one");
ls.Add("two");
string type = ls.Aggregate((x,y) => x + "," + y);

will yield one,two

if you need a space after the comma, simply change the last line to string type = ls.Aggregate((x,y) => x + ", " + y);

3 Comments

@Ozkan Please test it, and let us know.
actually, I hadn't checked. With only the two items in the List, no, they are equally as fast, but exploding it to 100,000 items, the Aggregate actually is far slower than string.Join. Good to know I guess. I've preferred Aggregate in the past, but I think I'll be moving to string.Join in the future
And, I found this throws an exception if the list is empty
4

Follow this:

       List<string> name = new List<string>();   

        name.Add("Latif");

        name.Add("Ram");

        name.Add("Adam");
        string nameOfString = (string.Join(",", name.Select(x => x.ToString()).ToArray()));

1 Comment

This can be simplified further. The ToArray and ToString are not necessary, and the following works: String nameOfString = String.Join(",", name.Select(x => x));
3
static void Main(string[] args)
{
   List<string> listStrings = new List<string>(){ "C#", "Asp.Net", "SQL Server", "PHP", "Angular"};
   string CommaSeparateString = GenerateCommaSeparateStringFromList(listStrings);
   Console.Write(CommaSeparateString);
   Console.ReadKey();
}

private static string GenerateCommaSeparateStringFromList(List<string> listStrings)
{
   return String.Join(",", listStrings);  
}

Convert a list of string to comma separated string C#.

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read this how-to-answer for providing quality answer.
0

That's the way I'd prefer to see if I was maintaining your code. If you manage to find a faster solution, it's going to be very esoteric, and you should really bury it inside of a method that describes what it does.

(does it still work without the ToArray)?

1 Comment

I can do string.Join only on Array's of strings. So it is necessary (because I don't use .NET 4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.