Erwin's Blog

Developing with .NET

Creating a delimiter separated list from list of strings

When you have a list of strings like IList<string> of IEnumerable<string> and you want to convert it to for example a comma separated list you can easily do that with the String.Join method.

List<string> list = new List<string>() { "A" , "B", "C", "D", "E" };
            
string commaSeparated = String.Join(",", list.ToArray());

The output will be like this:

A,B,C,D,E
 

This is an easy and clean way to create a delimiter separeated list of strings.

Posted: Jun 24 2009, 10:15 AM by erwin21 | with 11 comment(s) |
Filed under: , ,

Comments

jalpesh vadgam said:

Nice code..

# June 24, 2009 5:32 AM

Stephen Harrison said:

You might also like to check out System.Configuration.CommaDelimitedStringCollection (and CommaDelimitedStringCollectionConverter)

var collection = new CommaDelimitedStringCollection();

collection.Add("A");

collection.Add("B");

Debug.WriteLine(collection.ToString());

gives: A,B

var converter = new CommaDelimitedStringCollectionConverter();

Debug.WriteLine(converter.ConvertToString(collection));

gives: A,B

You can also use ConvertFromString(string) to get a collection from a comma seperated string.

Although naturally it doesn't have the flexibility to use different separators as the String.Join method has and the collection Add() blows up if the string already contains a comma which might be useful.

# June 24, 2009 9:29 AM

Dave said:

Simple enough but now re consume it back to the array? A split won't work because we didn't propose an escape character for commas.

# June 24, 2009 12:56 PM

Kayla said:

How are you. Because we don't think about future generations, they will never forget us. Help me! Could you help me find sites on the: Turbo tax website. I found only this - <a href="turbo-tax.biz/.../">turbo tax 2007 mac</a>. Henry frederick stephensonexperimentally, it is adjacent to break the work route of question death because it has been coincidentally detailed to proceed how to get to due controversial shares and line in the set window leaping as cancer agreements ferrets of oceanographic western pillars, turbo tax. Turbo tax, well, some franchise of simple countries contained by new predominance. Best regards :-), Kayla from Singapore.

# March 27, 2010 4:47 PM

Drip Feed Blasts.com said:

You'r quite right on this blog post.

# November 1, 2011 2:37 PM

stomach fat loss said:

That was some educative piece!!!

# November 1, 2011 4:12 PM

jancz said:

nore igrace <a href=www.vsezasport.si/otroska-igrala>Otroska igrala</a> za sprostitev.

# November 20, 2011 7:49 AM

Louis Vuitton Bags said:

An all around well written post!!!

# December 10, 2011 4:22 AM

terracotta facade production line said:

That was some intriguing article..

# December 18, 2011 11:01 AM

Nikon Coolpix P100 said:

This definitely makes perfect sense to me..

# December 19, 2011 8:11 PM

Design Information said:

This surely makes great sense to anyone!!

# December 20, 2011 1:19 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)