String.Join Method (String, String[])

July 28, 2014

Concatenates a specified separator String between each element of a specified String array, yielding a single concatenated string.

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

public static string Join(
	string separator,
	params string[] value
)

Parameters

separator
Type: System.String
The string to use as a separator.
value
Type: System.String []
An array that contains the elements to concatenate.

Return Value

Type: System.String
A string that consists of the elements in value delimited by the separator string. If value is an empty array, the method returns String.Empty.

ExceptionCondition
ArgumentNullException

value is null.

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

If separator is null, an empty string (Empty) is used instead. If any element in value is null, an empty string is used instead.

The following example demonstrates the Join method.


using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      outputBlock.Text += String.Format(MakeLine(0, 5, ", ")) + "\n";
      outputBlock.Text += String.Format(MakeLine(1, 6, "  ")) + "\n";
      outputBlock.Text += String.Format(MakeLine(9, 9, ": ")) + "\n";
      outputBlock.Text += String.Format(MakeLine(4, 7, "< ")) + "\n";
   }

   private static string MakeLine(int initVal, int multVal, string sep)
   {

      string[] sArr = new string[10];

      for (int i = initVal; i < initVal + 10; i++)
         sArr[i - initVal] = String.Format("{0,-3}", i * multVal);

      return String.Join(sep, sArr);
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft