System.String.Join Method

Concatenates the specified elements of a string array, using the specified separator between each element.

Syntax

public static string Join (string separator, string[] value, int startIndex, int count)

Parameters

separator
The string to use as a separator. separator is included in the returned string only if value has more than one element.
value
An array that contains the elements to concatenate.
startIndex
The first element in value to use.
count
The number of elements of value to use.

Returns

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

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

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 null, an empty string (string.Empty) is used instead. If any element in value is null, an empty string is used instead.

Example

The following example demonstrates the string.Join(string, String[]) method.

C# Example

using System;
public class StringJoin {
 public static void Main() {
 String[] strAry = { "Red" , "Green" , "Blue" };
 Console.WriteLine( String.Join( " :: ", strAry, 1, 2 ) );
 }
}

The output is

Green :: Blue

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0