Comma “izing” a list of items

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

    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

提交回复
热议问题