String.Join Method (String, array<String>[]()[], Int32, Int32)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Concatenates a specified separator String between each element of a specified String array, yielding a single concatenated string. Parameters specify the first array element and number of elements to use.

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

Syntax

Public Shared Function Join ( _
    separator As String, _
    value As String(), _
    startIndex As Integer, _
    count As Integer _
) As String
public static string Join(
    string separator,
    string[] value,
    int startIndex,
    int count
)

Parameters

  • value
    Type: array<System..::.String>[]()[]
    An array that contains the elements to concatenate.

Return Value

Type: System..::.String
A string that consists of the strings in value delimited by the separator string.
-or-
String..::.Empty if count is zero, value has no elements, or separator and all the elements of value are String..::.Empty.

Exceptions

Exception Condition
ArgumentNullException

value is nullNothingnullptra null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

startIndex or count is less than 0.

-or-

startIndex plus count is greater than the number of elements in value.

OutOfMemoryException

Out of memory.

Remarks

For example if separator is ", " and the elements of value are "apple", "orange", "grape", and "pear", Join(separator, value, 1, 2) returns "orange, grape".

If separator is nullNothingnullptra null reference (Nothing in Visual Basic), the empty string (Empty) is used instead. If any element in value is nullNothingnullptra null reference (Nothing in Visual Basic), an empty string is used instead.

Examples

The following code example concatenates two elements from an array of names of fruit.

' Sample for String.Join(String, String[], int int)
 _

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim val As [String]() = {"apple", "orange", "grape", "pear"}
      Dim sep As [String] = ", "
      Dim result As [String]

      outputBlock.Text += String.Format("sep = '{0}'", sep) & vbCrLf
      outputBlock.Text += String.Format("val() = {{'{0}' '{1}' '{2}' '{3}'}}", val(0), val(1), val(2), val(3)) & vbCrLf
      result = [String].Join(sep, val, 1, 2)
      outputBlock.Text += String.Format("String.Join(sep, val, 1, 2) = '{0}'", result) & vbCrLf
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'sep = ', '
'val() = {'apple' 'orange' 'grape' 'pear'}
'String.Join(sep, val, 1, 2) = 'orange, grape'
'
// Sample for String.Join(String, String[], int int)
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      String[] val = { "apple", "orange", "grape", "pear" };
      String sep = ", ";
      String result;

      outputBlock.Text += String.Format("sep = '{0}'", sep) + "\n";
      outputBlock.Text += String.Format("val[] = {{'{0}' '{1}' '{2}' '{3}'}}", val[0], val[1], val[2], val[3]) + "\n";
      result = String.Join(sep, val, 1, 2);
      outputBlock.Text += String.Format("String.Join(sep, val, 1, 2) = '{0}'", result) + "\n";
   }
}
/*
This example produces the following results:
sep = ', '
val[] = {'apple' 'orange' 'grape' 'pear'}
String.Join(sep, val, 1, 2) = 'orange, grape'
*/

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

String Class

Join Overload

System Namespace

Int32

Concat