String.Join Method (String, Object())

July 28, 2014

Concatenates the elements of an object array, using the specified separator between each element.

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

'Declaration
Public Shared Function Join ( _
	separator As String, _
	ParamArray values As Object() _
) As String

Parameters

separator
Type: System.String
The string to use as a separator.
values
Type: System.Object ()
An array that contains the elements to concatenate.

Return Value

Type: System.String
A string that consists of the elements of values delimited by the separator string. If values is an empty array, the method returns String.Empty.

ExceptionCondition
ArgumentNullException

values is Nothing.

If separator is Nothing, an empty string (String.Empty) is used instead. If any element of values other than the first element is null, an empty string (String.Empty) is used instead.

Join(String, Object()) is a convenience method that lets you concatenate each element in an object array without explicitly converting its elements to strings. The string representation of each object in the array is derived by calling that object's ToString method.

Notes to Callers

If the first element of values is Nothing, the Join method does not concatenate the elements in values but instead returns String.Empty. A number of workarounds for this issue are available. The easiest is to assign a value of String.Empty to the first element of the array, as the following example shows.


Dim values() As Object = {Nothing, "Cobb", 4189, 11434, 0.366}
If values(0) Is Nothing Then values(0) = String.Empty
outputBlock.Text += String.Join("|", values) & vbCrLf
' The example displays the following output:
'      |Cobb|4189|11434|0.366


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


Imports System.Collections.Generic

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim maxPrime As Integer = 100
      Dim primes() As Integer = GetPrimes(maxPrime)
      outputBlock.Text += String.Format("Primes less than {0}:", maxPrime) & vbCrLf
      outputBlock.Text += String.Format("   {0}", String.Join(" ", primes)) & vbCrLf
   End Sub

   Private Function GetPrimes(ByVal maxPrime As Integer) As Integer()
      Dim values(maxPrime) As Integer
      ' Use Sieve of Erathsthenes to determine prime numbers.
      For ctr As Integer = 2 To _
                           CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))))
         If values(ctr) = 1 Then Continue For

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

      Dim primes As New List(Of Integer)
      For ctr As Integer = 2 To values.GetUpperBound(0)
         If values(ctr) = 0 Then primes.Add(ctr)
      Next
      Return primes.ToArray()
   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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2014 Microsoft