.NET Framework Class Library
StringJoin Method (String, Object)

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/dd988350_en-us_VS_110_1_0_0_0_0%](False)> _
Public Shared Function Join ( _
	separator As [%$TOPIC/dd988350_en-us_VS_110_1_0_0_0_1%], _
	ParamArray values As [%$TOPIC/dd988350_en-us_VS_110_1_0_0_0_2%]() _
) As [%$TOPIC/dd988350_en-us_VS_110_1_0_0_0_3%]
[[%$TOPIC/dd988350_en-us_VS_110_1_0_1_0_0%](false)]
public static [%$TOPIC/dd988350_en-us_VS_110_1_0_1_0_1%] Join(
	[%$TOPIC/dd988350_en-us_VS_110_1_0_1_0_2%] separator,
	params [%$TOPIC/dd988350_en-us_VS_110_1_0_1_0_3%][] values
)
[[%$TOPIC/dd988350_en-us_VS_110_1_0_2_0_0%](false)]
public:
static [%$TOPIC/dd988350_en-us_VS_110_1_0_2_0_1%]^ Join(
	[%$TOPIC/dd988350_en-us_VS_110_1_0_2_0_2%]^ separator, 
	... array<[%$TOPIC/dd988350_en-us_VS_110_1_0_2_0_3%]^>^ values
)
[<[%$TOPIC/dd988350_en-us_VS_110_1_0_3_0_0%](false)>]
static member Join : 
        separator:[%$TOPIC/dd988350_en-us_VS_110_1_0_3_0_1%] * 
        values:[%$TOPIC/dd988350_en-us_VS_110_1_0_3_0_2%][] -> [%$TOPIC/dd988350_en-us_VS_110_1_0_3_0_3%]
public static function Join(
	separator : [%$TOPIC/dd988350_en-us_VS_110_1_0_4_0_0%], 
	... values : [%$TOPIC/dd988350_en-us_VS_110_1_0_4_0_1%][]
) : [%$TOPIC/dd988350_en-us_VS_110_1_0_4_0_2%]

Parameters

separator
Type: SystemString

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

values
Type: SystemObject

An array that contains the elements to concatenate.

Return Value

Type: SystemString
A string that consists of the elements of values delimited by the separator string. If values is an empty array, the method returns StringEmpty.
Exceptions
ExceptionCondition
ArgumentNullException

values is .

Remarks

If separator is or if any element of values other than the first element is , an empty string ( StringEmpty) is used instead. See the Notes for Callers section if the first element of values is .

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 , the Join(String, Object) method does not concatenate the elements in values but instead returns StringEmpty. A number of workarounds for this issue are available. The easiest is to assign a value of StringEmpty to the first element of the array, as the following example shows.

Dim values() As Object = { Nothing, "Cobb", 4189, 11434, .366 }
If values(0) Is Nothing Then values(0) = String.Empty
Console.WriteLine(String.Join("|", values))
' The example displays the following output: 
'      |Cobb|4189|11434|0.366
object[] values = { null, "Cobb", 4189, 11434, .366 };
if (values[0] == null) values[0] = String.Empty;
Console.WriteLine(String.Join("|", values));
// The example displays the following output: 
//      |Cobb|4189|11434|0.366
Examples

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.

Module Example
   Public Sub Main()
      Dim maxPrime As Integer = 100
      Dim primes() As 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 Integer()
      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 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)
      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
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      int maxPrime = 100;
      int[] primes = GetPrimes(maxPrime);
      Console.WriteLine("Primes less than {0}:", maxPrime);
      Console.WriteLine("   {0}", String.Join(" ", primes));
   }

   private static int[] GetPrimes(int maxPrime)
   {
      Array values = Array.CreateInstance(typeof(int), 
                              new int[] { maxPrime - 1}, new int[] { 2 }); 
      // Use Sieve of Eratosthenes to determine prime numbers. 
      for (int ctr = values.GetLowerBound(0); ctr <= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
      {

         if ((int) values.GetValue(ctr) == 1) continue;

         for (int multiplier = ctr; multiplier <=  maxPrime / 2; multiplier++)
            if (ctr * multiplier <= maxPrime)
               values.SetValue(1, ctr * multiplier);
      }      

      List<int> primes = new List<int>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr);
      return primes.ToArray();
   }   
}
// 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
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8