本文為機器翻譯文章。如需檢視英文版,請選取 [原文] 核取方塊。您也可以將滑鼠指標移到文字上,即可在快顯視窗顯示英文原文。
譯文
原文

String.Join<T> 方法 (String, IEnumerable<T>)

 

串連集合的成員,並在每個成員之間使用指定的分隔符號。

命名空間:   System
組件:  mscorlib (在 mscorlib.dll 中)

[ComVisibleAttribute(false)]
public static string Join<T>(
	string separator,
	IEnumerable<T> values
)

參數

separator
Type: System.String

要當做分隔符號的字串。只有在 values 有一個以上項目時,separator 才會包含在傳回的字串中。

values
Type: System.Collections.Generic.IEnumerable<T>

包含要串連之物件的集合。

傳回值

Type: System.String

字串,由 values 中的成員組成,且每個成員之間都會以 separator 字串分隔。 如果 values 沒有成員,方法會傳回 String.Empty

類型參數

T

values 之成員的類型。

Exception Condition
ArgumentNullException

valuesnull

If separator is null, an empty string (F:System.String.Empty) is used instead. If any member of values is null, an empty string is used instead.

Join<T>(String, IEnumerable<T>) is a convenience method that lets you concatenate each member of an IEnumerable<T> collection without first converting them to strings. The string representation of each object in the IEnumerable<T> collection is derived by calling that object's ToString method.

This method is particular useful with Language-Integrated Query (LINQ) query expressions. For example, the following code defines a very simple Animal class that contains the name of an animal and the order to which it belongs. It then defines a T:System.Collections.Generic.List`1 object that contains a number of Animal objects. The M:System.Linq.Enumerable.Where``1(System.Collections.Generic.IEnumerable{``0},System.Func{``0,System.Boolean}) extension method is called to extract the Animal objects whose Order property equals "Rodent". The result is passed to the M:System.String.Join``1(System.String,System.Collections.Generic.IEnumerable{``0}) method.

using System;
using System.Collections.Generic;
using System.Linq;

public class Animal
{
   public string Kind;
   public string Order;

   public Animal(string kind, string order)
   {
      this.Kind = kind;
      this.Order = order;
   }

   public override string ToString()
   {
      return this.Kind;
   }
}

public class Example
{
   public static void Main()
   {
      List<Animal> animals = new List<Animal>();
      animals.Add(new Animal("Squirrel", "Rodent"));
      animals.Add(new Animal("Gray Wolf", "Carnivora"));
      animals.Add(new Animal("Capybara", "Rodent"));
      string output = String.Join(" ", animals.Where( animal => 
                      (animal.Order == "Rodent")));
      Console.WriteLine(output);  
   }
}
// The example displays the following output:
//      Squirrel Capybara

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 T:System.Collections.Generic.List`1 object of type integer, which it then passes to the M:System.String.Join``1(System.String,System.Collections.Generic.IEnumerable{``0}) method.

using System;
using System.Collections.Generic;

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

   private static List<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;
   }   
}
// 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

Universal Windows Platform
自 4.5 起可用
.NET Framework
自 4.0 起可用
Portable Class Library
支援版本:portable .NET platforms
Silverlight
自 4.0 起可用
Windows Phone Silverlight
自 7.1 起可用
Windows Phone
自 8.1 起可用
回到頁首
顯示: