本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

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

 

串联对象数组的各个元素,其中在每个元素之间使用指定的分隔符。

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

[ComVisibleAttribute(false)]
public static string Join(
	string separator,
	params object[] values
)

参数

separator
Type: System.String

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

values
Type: System.Object[]

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

返回值

Type: System.String

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

Exception Condition
ArgumentNullException

valuesnull

If separator is null or if any element of values other than the first element is null, an empty string (F:System.String.Empty) is used instead. See the Notes for Callers section if the first element of values is null.

Join(String, Object[]) is a convenience method that lets you concatenate each element in an object array without explicitly converting its elements to strings. The string representation of each object in the array is derived by calling that object's ToString method.

调用函数说明:

If the first element of values is null, the M:System.String.Join(System.String,System.Object[]) method does not concatenate the elements in values but instead returns F:System.String.Empty. A number of workarounds for this issue are available. The easiest is to assign a value of F:System.String.Empty to the first element of the array, as the following example shows.

object[] values = { null, "Cobb", 4189, 11434, .366 };
if (values[0] == null) values[0] = String.Empty;
Console.WriteLine(String.Join("|", values));
// The example displays the following output:
//      |Cobb|4189|11434|0.366

The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. It assigns the result to a integer array, which it then passes to the M:System.String.Join(System.String,System.Object[]) method.

using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      int maxPrime = 100;
      int[] primes = GetPrimes(maxPrime);
      Console.WriteLine("Primes less than {0}:", maxPrime);
      Console.WriteLine("   {0}", String.Join(" ", primes));
   }

   private static int[] GetPrimes(int maxPrime)
   {
      Array values = Array.CreateInstance(typeof(int), 
                              new int[] { maxPrime - 1}, new int[] { 2 }); 
      // Use Sieve of Eratosthenes to determine prime numbers.
      for (int ctr = values.GetLowerBound(0); ctr <= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
      {

         if ((int) values.GetValue(ctr) == 1) continue;

         for (int multiplier = ctr; multiplier <=  maxPrime / 2; multiplier++)
            if (ctr * multiplier <= maxPrime)
               values.SetValue(1, ctr * multiplier);
      }      

      List<int> primes = new List<int>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr);
      return primes.ToArray();
   }   
}
// The example displays the following output:
//    Primes less than 100:
//       2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

通用 Windows 平台
自 8 起可用
.NET Framework
自 4.0 起可用
可移植类库
可移植 .NET 平台 中受支持
Silverlight
自 4.0 起可用
Windows Phone Silverlight
自 7.1 起可用
Windows Phone
自 8.1 起可用
返回页首
显示: