共用方式為


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: January 2011

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)

Syntax

'Declaration
Public Shared Function Join ( _
    separator As String, _
    ParamArray value As String() _
) As String
public static string Join(
    string separator,
    params string[] value
)

Parameters

  • separator
    Type: System.String
    The string to use as a separator.
  • value
    Type: array<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.

Exceptions

Exception Condition
ArgumentNullException

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

Remarks

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

Examples

The following example demonstrates the Join method.


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text += String.Format(MakeLine(0, 5, ", ")) & vbCrLf
      outputBlock.Text += String.Format(MakeLine(1, 6, "  ")) & vbCrLf
      outputBlock.Text += String.Format(MakeLine(9, 9, ": ")) & vbCrLf
      outputBlock.Text += String.Format(MakeLine(4, 7, "< ")) & vbCrLf
   End Sub 'Main


   Private Shared Function MakeLine(ByVal initVal As Integer, ByVal multVal As Integer, ByVal sep As String) As String
      Dim sArr(10) As String
      Dim i As Integer


      For i = initVal To (initVal + 10) - 1
         sArr((i - initVal)) = [String].Format("{0,-3}", i * multVal)

      Next i
      Return [String].Join(sep, sArr)
   End Function 'MakeLine
End Class 'JoinTest
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);
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

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

January 2011

Noted that an empty string is substituted for any null element in the array.

Customer feedback.