ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToDelimitedString

Converts a List(Of String) into a delimited string.

Source

''' <summary>
''' Returns a delimited string from the list.
''' </summary>
''' <param name="ls"></param>
''' <param name="delimiter"></param>
''' <returns></returns>
''' <remarks>
''' </remarks>
<Extension()> _
Public Function ToDelimitedString(ByVal ls As List(Of String), ByVal delimiter As String) As String
    Dim sb As New StringBuilder

    For Each buf As String In ls
        sb.Append(buf)
        sb.Append(delimiter)
    Next

    ' The final delimiter is trimmed off since there is no record after that item
    Return sb.ToString.Trim(delimiter)
End Function

Example

Dim ls As New List(Of String)
ls.Add("Blake")
ls.Add("Bob")
ls.Add("William")
ls.Add("Jacob")

Dim myString As String = ls.ToDelimitedString(",")

Author: Blake Pell

Submitted on: 25 mrt. 2010

Language: VB

Type: System.String<T>

Views: 5227