String.Join方法(string,string[])

串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符。

命名空间:                   System
程序集:         mscorlib(位于 mscorlib.dll)

语法:

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

参数
  • separator

  • Type:    System.String

    要用作分隔符的字符串。 只有在 separator 具有多个元素时,value 才包括在返回的字符串中。

  • value

  • Type:    System.String[]

    一个数组,其中包含要连接的元素。

返回值

Type:    System.String

一个由 value 中的元素组成的字符串,这些元素以 separator 字符串分隔。 如果 value 为空数组,该方法将返回 String.Empty

异常

ExceptionCondition
ArgumentNullException

 valuenull

备注

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

示例

The following example demonstrates the M:System.String.Join(System.String,System.String[]) method.

using System;public class JoinTest {   
 public static void Main() {
        Console.WriteLine(MakeLine(0, 5, ", "));
        Console.WriteLine(MakeLine(1, 6, "  "));
        Console.WriteLine(MakeLine(9, 9, ": "));
        Console.WriteLine(MakeLine(4, 7, "< "));
    }   
     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);
    }
}
// The example displays the following output:
//       0  , 5  , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45
//       6    12   18   24   30   36   42   48   54   60
//       81 : 90 : 99 : 108: 117: 126: 135: 144: 153: 162
//       28 < 35 < 42 < 49 < 56 < 63 < 70 < 77 < 84 < 91


版本信息

通用 Windows 平台
自 8 起可用
.NET Framework
自 1.1 起可用
可移植类库
在        可移植 .NET 平台       中受支持
Silverlight
自 2.0 起可用
Windows Phone Silverlight
自 7.0 起可用
Windows Phone
自 8.1 起可用


备注:转自:https://msdn.microsoft.com/zh-cn/library/57a79xd0(v=vs.110).aspx