Comma “izing” a list of items

前端 未结 10 1693
我寻月下人不归
我寻月下人不归 2021-02-04 08:53

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

相关标签:
10条回答
  • 2021-02-04 09:42

    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.

    0 讨论(0)
  • 2021-02-04 09:49

    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.

    0 讨论(0)
  • 2021-02-04 09:53

    Does the solution have to use a StringBuilder or the Concat method?

    If not, you could use the static String.Join method. For example (in C#):

    string result = String.Join(",", items.ToArray());
    

    See my very similar question for more details on this.

    0 讨论(0)
  • 2021-02-04 09:54

    If you don't have to use StringBuilder or Concat method you could also use:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Configuration;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                CommaDelimitedStringCollection commaStr = new CommaDelimitedStringCollection();
                string[] itemList = { "Test1", "Test2", "Test3" };
                commaStr.AddRange(itemList);
                Console.WriteLine(commaStr.ToString()); //Outputs Test1,Test2,Test3
                Console.ReadLine();
            }
        }
    }
    

    This requires a reference to System.Configuration

    0 讨论(0)
提交回复
热议问题