Convert An Array To A Delimited String

There are many times when I need to convert an array to a delimited string. Here is an easy way to do it using generics in .NET 2.0.

  Public Shared Function ConvertArrayToString(Of arrayType As {IEnumerable})(ByVal array As arrayType(), ByVal separator As Char) As String

 

      If Not IsValidArray(array) Then

        Throw New ArgumentNullException(“array”)

      End If

 

      If IsNothing(separator) Then

        Throw New ArgumentNullException(“separator”)

      End If

 

      Dim result As New System.Text.StringBuilder

 

      For Each arrayItem As arrayType In array

        If result.Length > 0 Then

          result.Append(separator)

        End If

 

        result.Append(arrayItem.ToString)

 

      Next

 

      Return result.ToString

 

  End Function

Tips By: David McCarter

This code can be found in the open source dotNetTips.Utility assembly


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.