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

Concatenates the members of a constructed IEnumerable(Of T) collection of type String, using the specified separator between each member.

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

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

Parameters

separator
Type: System.String

The string to use as a separator. separator is included in the returned string only if values has more than one element.

values
Type: System.Collections.Generic.IEnumerable(Of String)

A collection that contains the strings 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.

ExceptionCondition
ArgumentNullException

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(String, IEnumerable(Of String)) is a convenience method that lets you concatenate each element in an IEnumerable(Of String) collection without first converting the elements to a string array. It is particularly useful with Language-Integrated Query (LINQ) query expressions. The following example passes a List(Of String) object that contains either the uppercase or lowercase letters of the alphabet to a lambda expression that selects letters that are equal to or greater than a particular letter (which, in the example, is "M"). The IEnumerable(Of String) collection returned by the Enumerable.Where method is passed to the Join(String, IEnumerable(Of String)) method to display the result as a single string.

Imports System.Collections.Generic
Imports System.Linq

Module modMain
   Public Sub Main()
      Dim output As String = String.Join(" ", GetAlphabet(True).Where(Function(letter) _
                                                         letter >= "M"))

      Console.WriteLine(output)                                     
   End Sub 

   Private Function GetAlphabet(upper As Boolean) As List(Of String)
      Dim alphabet As New List(Of String)
      Dim charValue As Integer = CInt(IIf(upper, 65, 97))
      For ctr As Integer = 0 To 25
         alphabet.Add(ChrW(charValue + ctr).ToString())
      Next 
      Return alphabet 
   End Function 
End Module 
' The example displays the following output: 
'      M N O P Q R S T U V W X Y Z

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 String, which it then passes to the Join(String, IEnumerable(Of String)) method.

Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim maxPrime As Integer = 100
      Dim primes As List(Of String) = 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 String)
      Dim values As Array = Array.CreateInstance(GetType(Integer), _
                              New Integer() { maxPrime - 1}, New Integer(){ 2 }) 
        ' Use Sieve of Eratosthenes to determine prime numbers. 
      For ctr As Integer = values.GetLowerBound(0) To _
                           CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))))
         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)
         Next    
      Next       

      Dim primes As New List(Of String)
      For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
         If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr.ToString())
      Next             
      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.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft