Comma Delimiting Strings in .NET - The Framework Way

by Donn Felker 14. November 2007 12:25

It's common to see a developer use a StringBuilder or basic string concatenation to concatenate strings in .NET code.

You'll usually see something like this:

string comma = ",";
StringBuilder sb = new StringBuilder();
sb.Append("Donn");
sb.Append(comma);
sb.Append("Felker");
sb.Append(comma);
sb.Append("Jiu-Jitsu");
sb.ToString(); 

And this will return "Donn,Felker,Jiu-Jitsu"

But the .NET framework has something built in just for doing this type of operation.

Its called the CommaDelimitedStringCollection and it's located in System.Configuration namespace. What it allows you to do is to add items to a collection and then just access the "ToString" method of the class to return the value of the collection, delimited by comma's.

Here it is, in action:

CommaDelimitedStringCollection coll = new CommaDelimitedStringCollection();
coll.Add("Donn");
coll.Add("Felker");
coll.Add("Jiu-Jitsu");
coll.ToString();

This will return "Donn,Felker,Jiu-Jitsu".

Much easier, much nicer.

Enjoy.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Donn Felker

Senior Consultant
MCTS
ScrumMaster
Agile Practitioner

About Me | Books I Recommend

Gotta Pay The Bills


Tag cloud

    Popular Posts

    RecentComments

    Comment RSS

    Calendar

    <<  January 2009  >>
    MoTuWeThFrSaSu
    2930311234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678

    View posts in large calendar