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
Thanks for all of the responses.
It appears that the "proper" answer depends on the context in which the comma separated list is being built. I don't have a neat list of items to use (had to use something for an example...), but I do have an array whose items may or may not be added to the comma seperated list depending on various conditions.
So I chose something to the effect of
strResult = ""
strSeparator = ""
for i as integer = 0 to arrItems.Length - 1
if arrItems(i) <> "test" and arrItems(i) <> "point" then
strResult = strResult & strSeparator & arrItem(i)
strSeparator = ", "
end if
next
As usual, there are numerous methods of doing this. I don't know that any one method deserves more praise or promotion than another. Some are more useful in certain contexts while others satisfy the requirements of different contexts.
Again, thanks to all for your input.
BTW, the original post with the "off the top of my head" code sample is not filtering zero length items, instead it is waiting for the result string to become greater than zero length before adding the comma. Probably not very efficient but I have'nt tested it. Again, it was off the top of my head.