Comma “izing” a list of items

2020-05-24 00: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 or String Concat.)

Dim strResult As String = ""
Dim lstItems As New List(Of String)
lstItems.Add("Hello")
lstItems.Add("World")
For Each strItem As String In lstItems
    If strResult.Length > 0 Then
        strResult = strResult & ", "
    End If
    strResult = strResult & strItem
Next
MessageBox.Show(strResult)

10条回答
够拽才男人
2楼-- · 2020-05-24 01:26

or you can do:

Separator = ""
For Each Item In Collection
  Add Separator + Item To String
  Separator = ", "

By setting the separator to an empty string in the first iteration you skip the first comma. One less if statement. This might or might not be more readable depending on what you're used to

查看更多
家丑人穷心不美
3楼-- · 2020-05-24 01:33

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

查看更多
冷血范
4楼-- · 2020-05-24 01:37
Dim strResult As String = ""
Dim separator = ","
Dim lstItems As New List(Of String)
lstItems.Add("Hello")
lstItems.Add("World")
For Each strItem As String In lstItems
     strResult = String.Concat(strResult, separator)
Next
strResult = strResult.TrimEnd(separator.ToCharArray())
MessageBox.Show(strResult)

The idea is to use String.TrimEnd() function

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-05-24 01:43

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.

查看更多
Melony?
6楼-- · 2020-05-24 01:44

Going on from the String.Join answer, to ignore null/empty strings (and if you are using .NET 3.5) you could use a bit of Linq. e.g.

Dim Result As String
Dim Items As New List(Of String)
Items.Add("Hello")
Items.Add("World")
Result = String.Join(",", Items.ToArray().Where(Function(i) Not String.IsNullOrEmpty(i))
MessageBox.Show(Result)
查看更多
男人必须洒脱
7楼-- · 2020-05-24 01:45

Would you believe there's a class in the .NET framework which provides this functionality?

public static string ListToCsv<T>(List<T> list)
        {
            CommaDelimitedStringCollection commaStr = new CommaDelimitedStringCollection();

            list.ForEach(delegate(T item)
            {
                commaStr.Add(item.ToString());
            });


            return commaStr.ToString();
        }
查看更多
登录 后发表回答