Given a list of strings, what is the best method for concatenating these strings into a comma separated list with no comma at the end. (VB.NET or C#) (Using either StringBuilder
There are several ways to do this, but they're basically variations on a theme.
Pseudocode:
For Each Item In Collection:
Add Item To String
If Not Last Item, Add Comma
A different way that I like a little better is something like this:
For Each Item In Collection:
If Not First Item, Add Comma
Add Item To String
Edit: The reason I like the second way of doing it is that each item stands on its own. Using the first approach, if you modified your logic later so that a subsequent item might not get added, you could end up with a stray comma at the end of the string unless you also made your test in the previous item more intelligent, which is dumb.