String.Join Method (String, array<Object[])
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Concatenates the elements of an object array, using the specified separator between each element.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<ComVisibleAttribute(False)> _
Public Shared Function Join ( _
separator As String, _
ParamArray values As Object() _
) As String
[ComVisibleAttribute(false)]
public static string Join(
string separator,
params Object[] values
)
Parameters
- separator
Type: System.String
The string to use as a separator.
- values
Type: array<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.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | values is nulla null reference (Nothing in Visual Basic). |
Remarks
If separator is nulla null reference (Nothing in Visual Basic), 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, array<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 nulla null reference (Nothing in Visual Basic), 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
object[] values = { null, "Cobb", 4189, 11434, .366 };
if (values[0] == null) values[0] = String.Empty;
outputBlock.Text += String.Join("|", values) + "\n";
// 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, array<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
using System;
using System.Collections.Generic;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
int maxPrime = 100;
int[] primes = GetPrimes(maxPrime);
outputBlock.Text += String.Format("Primes less than {0}:", maxPrime) + "\n";
outputBlock.Text += String.Format(" {0}", String.Join(" ", primes)) + "\n";
}
private static int[] GetPrimes(int maxPrime)
{
int[] values = new int[maxPrime + 1];
// Use Sieve of Erathsthenes to determine prime numbers.
for (int ctr = 2; ctr <= (int)Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
{
if (values[ctr] == 1) continue;
for (int multiplier = ctr; multiplier <= maxPrime / 2; multiplier++)
if (ctr * multiplier < maxPrime + 1)
values[ctr * multiplier] = 1;
}
List<int> primes = new List<int>();
for (int ctr = 2; ctr <= values.GetUpperBound(0); ctr++)
if (values[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
Silverlight
Supported in: 5, 4
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Change History
Date |
History |
Reason |
---|---|---|
Clarified the effect of elements with null values in the Remarks section. |
Content bug fix. |
|
January 2011 |
Added the Notes for Callers section. |
Content bug fix. |