Comma “izing” a list of items

前端 未结 10 1698
我寻月下人不归
我寻月下人不归 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: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

提交回复
热议问题