Join メソッド (String, Object[])
この記事は機械翻訳されたものです。 記事の文章にポインターを重ねると、原文のテキストが表示されます。 詳細情報

String.Join メソッド (String, Object[])



名前空間:   System
アセンブリ:  mscorlib (mscorlib.dll 内)

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


Type: System.String

区切り文字として使用する文字列。戻される文字列に separator が含まれるのは、values に複数の要素がある場合のみです。

Type: System.Object[]



Type: System.String

values の要素からなる、separator 文字列で区切られた文字列。values が空の配列の場合、メソッドは String.Empty を返します。

Exception Condition

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) 
      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

Universal Windows Platform
4.5 から利用可能
.NET Framework
4.0 から利用可能
Portable Class Library
サポート対象 : portable .NET platforms
4.0 から利用可能
Windows Phone Silverlight
7.1 から利用可能
Windows Phone
8.1 から利用可能
© 2015 Microsoft