Comma “izing” a list of items

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

    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

提交回复
热议问题