This documentation is archived and is not being maintained.

String.Join(Of T) Method (String, IEnumerable(Of T))

Concatenates the members of a collection, using the specified separator between each member.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

<ComVisibleAttribute(False)> _
Public Shared Function Join(Of T) ( _
	separator As String, _
	values As IEnumerable(Of T) _
) As String

Type Parameters


The type of the members of values.


Type: System.String
The string to use as a separator.
Type: System.Collections.Generic.IEnumerable(Of T)
A collection that contains the objects to concatenate.

Return Value

Type: System.String
A string that consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.


values is Nothing.

If separator is Nothing, an empty string (String.Empty) is used instead. If any member of values is Nothing, an empty string is used instead.

Join(Of T)(String, IEnumerable(Of T)) is a convenience method that lets you concatenate each member of an IEnumerable(Of T) collection without first converting them to strings. The string representation of each object in the IEnumerable(Of T) collection is derived by calling that object's ToString method.

This method is particular useful with Language-Integrated Query (LINQ) query expressions. For example, the following code defines a very simple Animal class that contains the name of an animal and the order to which it belongs. It then defines a List(Of T) object that contains a number of Animal objects. The Enumerable.Where extension method is called to extract the Animal objects whose Order property equals "Rodent". The result is passed to the Join(Of T)(String, IEnumerable(Of T)) method.

Imports System.Collections.Generic

Public Class Animal
   Public Kind As String
   Public Order As String

   Public Sub New(kind As String, order As String)
      Me.Kind = kind
      Me.Order = order
   End Sub

   Public Overrides Function ToString() As String
      Return Me.Kind
   End Function
End Class

Module Example
   Public Sub Main()
      Dim animals As New List(Of Animal)
      animals.Add(New Animal("Squirrel", "Rodent"))
      animals.Add(New Animal("Gray Wolf", "Carnivora"))
      animals.Add(New Animal("Capybara", "Rodent")) 
      Dim output As String = String.Join(" ", animals.Where(Function(animal) _
                                           animal.Order = "Rodent"))
   End Sub
End Module
' The example displays the following output:
'      Squirrel Capybara

The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. It assigns the result to a List(Of T) object of type integer, which it then passes to the Join(Of T)(String, IEnumerable(Of T)) method.

Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim maxPrime As Integer = 100
      Dim primes As List(Of Integer) = GetPrimes(maxPrime)
      Console.WriteLine("Primes less than {0}:", maxPrime)
      Console.WriteLine("   {0}", String.Join(" ", primes))
   End Sub

   Private Function GetPrimes(maxPrime As Integer) As List(Of Integer)
      Dim values As Array = Array.CreateInstance(GetType(Integer), _
                              New Integer() { maxPrime - 1}, New Integer(){ 2 }) 
      ' Use Sieve of Erathsthenes to determine prime numbers.
      For ctr As Integer = values.GetLowerBound(0) To _
         If CInt(values.GetValue(ctr)) = 1 Then Continue For

         For multiplier As Integer = ctr To maxPrime \ 2
            If ctr * multiplier <= maxPrime Then values.SetValue(1, ctr * multiplier)

      Dim primes As New System.Collections.Generic.List(Of Integer)
      For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
         If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr)
      Return primes
   End Function   
End Module
' The example displays the following output:
'    Primes less than 100:
'       2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.