Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to concatenate list of strings into a comma-separated strings, where strings are members of an object?

Say I have a List(Of Tag) with Tag being an object. One member of Tag, Tag.Description, is a string, and I want to make a comma-separated concatenation of the Description members.

Is there an easier way to do this than to read the Description members into a List(Of String) and then use the Join function?

Thanks!

like image 652
John Avatar asked Oct 16 '09 21:10

John


2 Answers

Try this:

String.Join(", ", tagList.Select(t => t.Description).ToArray());

Sorry, I just read again and saw you're using VS2005; so maybe best way is to create a StringBuilder and concatenate your tag.Description.

like image 119
Rubens Farias Avatar answered Oct 13 '22 09:10

Rubens Farias


Here's a Visual Studio 2005 Solution

Public Function ConcatDescription(ByVal list As List(Of Tag) As String
  Dim builder as New StringBuilder
  Dim isFirst As Boolean = True
  For Each t As Tag in list   
    If Not isFirst Then
      builder.Append(","c)
    End If
    builder.Append(t.Description)
    isFirst = False
  Next
  Return builder.ToString()
End Function
like image 29
JaredPar Avatar answered Oct 13 '22 09:10

JaredPar