Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

String.Join Method (String, String[], Int32, Int32)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static string Join(
	string separator,
	string[] value,
	int startIndex,
	int count
)

Parameters

separator
Type: System.String
The string to use as a separator.
value
Type: System.String[]
An array that contains the elements to concatenate.
startIndex
Type: System.Int32
The first element in value to use.
count
Type: System.Int32
The number of elements of value to use.

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.
ExceptionCondition
ArgumentNullException

value is null.

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.

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.

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


// Sample for String.Join(String, String[], int int)
using System;

class Sample {
    public static void Main() {
    String[] val = {"apple", "orange", "grape", "pear"};
    String sep   = ", ";
    String result;

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.