英語で読む

次の方法で共有


String.Concat メソッド

定義

Stringの 1 つ以上のインスタンス、または Objectの 1 つ以上のインスタンスの値の String 表現を連結します。

オーバーロード

Concat(String, String, String, String)

Stringの指定された 4 つのインスタンスを連結します。

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

指定された 4 つの読み取り専用文字スパンの文字列表現を連結します。

Concat(Object, Object, Object, Object)

指定した 4 つのオブジェクトと、省略可能な可変長パラメーター リストで指定されたオブジェクトの文字列形式を連結します。

Concat(String, String, String)

Stringの 3 つの指定されたインスタンスを連結します。

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

指定された 3 つの読み取り専用文字スパンの文字列形式を連結します。

Concat(Object, Object, Object)

指定された 3 つのオブジェクトの文字列形式を連結します。

Concat(String, String)

Stringの 2 つの指定されたインスタンスを連結します。

Concat(Object)

指定したオブジェクトの文字列形式を作成します。

Concat(Object, Object)

指定された 2 つのオブジェクトの文字列形式を連結します。

Concat(String[])

指定した String 配列の要素を連結します。

Concat(ReadOnlySpan<String>)

Stringの指定したスパンの要素を連結します。

Concat(ReadOnlySpan<Object>)

オブジェクトの指定されたスパン内の要素の文字列表現を連結します。

Concat(Object[])

指定した Object 配列内の要素の文字列形式を連結します。

Concat(IEnumerable<String>)

String型の構築された IEnumerable<T> コレクションのメンバーを連結します。

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

指定された 2 つの読み取り専用文字スパンの文字列表現を連結します。

Concat<T>(IEnumerable<T>)

IEnumerable<T> 実装のメンバーを連結します。

注釈

注意

C# と F# の + などの言語の文字列連結演算子や、Visual Basic の &+ を使用して文字列を連結することもできます。 どちらのコンパイラも、連結演算子を String.Concatのオーバーロードの 1 つへの呼び出しに変換します。

Concat(String, String, String, String)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

Stringの指定された 4 つのインスタンスを連結します。

C#
public static string Concat (string str0, string str1, string str2, string str3);
C#
public static string Concat (string? str0, string? str1, string? str2, string? str3);

パラメーター

str0
String

連結する最初の文字列。

str1
String

連結する 2 番目の文字列。

str2
String

連結する 3 番目の文字列。

str3
String

連結する 4 番目の文字列。

戻り値

str0str1str2、および str3の連結。

次の例では、4 文字の単語の配列を定義し、文字列配列に個々の文字を格納して、文字を取り出します。 次に、Concat(String, String, String, String) メソッドを呼び出して、スクランブリングされた単語を再構成します。

C#
using System;
using System.Collections;

public class Example
{
   public static void Main()
   {
      const int WORD_SIZE = 4;
      
      // Define some 4-letter words to be scrambled.
      string[] words = { "home", "food", "game", "rest" };
      // Define two arrays equal to the number of letters in each word.
      double[] keys = new double[WORD_SIZE];
      string[] letters = new string[WORD_SIZE];
      // Initialize the random number generator.
      Random rnd = new Random();
      
      // Scramble each word.
      foreach (string word in words)
      {
         for (int ctr = 0; ctr < word.Length; ctr++)
         {
            // Populate the array of keys with random numbers.
            keys[ctr] = rnd.NextDouble();
            // Assign a letter to the array of letters.
            letters[ctr] = word[ctr].ToString();
         }   
         // Sort the array. 
         Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);      
         // Display the scrambled word.
         string scrambledWord = String.Concat(letters[0], letters[1], 
                                              letters[2], letters[3]);
         Console.WriteLine("{0} --> {1}", word, scrambledWord);
      } 
   }
}
// The example displays output like the following:
//       home --> mheo
//       food --> oodf
//       game --> aemg
//       rest --> trse

注釈

メソッドは、str0str1str2、および str3を連結します。区切り記号は追加されません。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定された 4 つの読み取り専用文字スパンの文字列表現を連結します。

C#
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2, ReadOnlySpan<char> str3);

パラメーター

str0
ReadOnlySpan<Char>

連結する最初の読み取り専用文字スパン。

str1
ReadOnlySpan<Char>

連結する 2 番目の読み取り専用文字スパン。

str2
ReadOnlySpan<Char>

連結する 3 番目の読み取り専用文字スパン。

str3
ReadOnlySpan<Char>

連結する 4 番目の読み取り専用文字スパン。

戻り値

str0str1str2str3の値の連結された文字列形式。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat(Object, Object, Object, Object)

重要

この API は CLS 準拠ではありません。

指定した 4 つのオブジェクトと、省略可能な可変長パラメーター リストで指定されたオブジェクトの文字列形式を連結します。

C#
[System.CLSCompliant(false)]
public static string Concat (object arg0, object arg1, object arg2, object arg3);

パラメーター

arg0
Object

連結する最初のオブジェクト。

arg1
Object

連結する 2 番目のオブジェクト。

arg2
Object

連結する 3 番目のオブジェクト。

arg3
Object

連結する 4 番目のオブジェクト。

戻り値

パラメーター リスト内の各値の連結された文字列形式。

属性

次の例は、Concat(Object, Object, Object, Object) メソッドを使用して変数パラメーターの一覧を連結する方法を示しています。 この場合、メソッドは 9 つのパラメーターを使用して呼び出されます。

C#
using System;
using System.Collections;

public class Example
{
   public static void Main()
   {
      const int WORD_SIZE = 4;
      
      // Define some 4-letter words to be scrambled.
      string[] words = { "home", "food", "game", "rest" };
      // Define two arrays equal to the number of letters in each word.
      double[] keys = new double[WORD_SIZE];
      string[] letters = new string[WORD_SIZE];
      // Initialize the random number generator.
      Random rnd = new Random();
      
      // Scramble each word.
      foreach (string word in words)
      {
         for (int ctr = 0; ctr < word.Length; ctr++)
         {
            // Populate the array of keys with random numbers.
            keys[ctr] = rnd.NextDouble();
            // Assign a letter to the array of letters.
            letters[ctr] = word[ctr].ToString();
         }   
         // Sort the array. 
         Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);      
         // Display the scrambled word.
         string scrambledWord = String.Concat(letters[0], letters[1], 
                                              letters[2], letters[3]);
         Console.WriteLine("{0} --> {1}", word, scrambledWord);
      } 
   }
}
// The example displays output like the following:
//       home --> mheo
//       food --> oodf
//       game --> aemg
//       rest --> trse

注釈

注意

この API は CLS に準拠していません。 CLS 準拠の代替手段は String.Concat(Object[])です。 C# コンパイラと Visual Basic コンパイラは、このメソッドの呼び出しを String.Concat(Object[])の呼び出しとして自動的に解決します。

このメソッドは、パラメーターなしの ToString メソッドを呼び出すことによって、パラメーター リスト内の各オブジェクトを連結します。区切り記号は追加されません。

String.Empty は、null 引数の代わりに使用されます。

注意

Concat メソッドの最後のパラメーターは、連結する 1 つ以上の追加オブジェクトの省略可能なコンマ区切りリストです。

注意 (呼び出し元)

このメソッドは、vararg キーワードでマークされます。これは、可変数のパラメーターをサポートしていることを意味します。 このメソッドは Visual C++ から呼び出すことができますが、C# または Visual Basic コードから呼び出すことはできません。 C# および Visual Basic コンパイラは、Concat(Object[])の呼び出しとして Concat(Object, Object, Object, Object) の呼び出しを解決します。

適用対象

.NET Framework 4.8.1 およびその他のバージョン
製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Concat(String, String, String)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

Stringの 3 つの指定されたインスタンスを連結します。

C#
public static string Concat (string str0, string str1, string str2);
C#
public static string Concat (string? str0, string? str1, string? str2);

パラメーター

str0
String

連結する最初の文字列。

str1
String

連結する 2 番目の文字列。

str2
String

連結する 3 番目の文字列。

戻り値

str0str1、および str2の連結。

次の例では、Concat メソッドを使用して 3 つの文字列を連結し、結果を表示します。

C#
using System;

public class Example
{
   public static void Main()
   {
      String s1 = "We went to a bookstore, ";
      String s2 = "a movie, ";
      String s3 = "and a restaurant.";

      var s = String.Concat(s1, s2, s3);
      Console.WriteLine(s);
   }
}
// The example displays the following output:
//      We went to a bookstore, a movie, and a restaurant.

注釈

メソッドは、str0str1、および str2を連結します。区切り記号は追加されません。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定された 3 つの読み取り専用文字スパンの文字列形式を連結します。

C#
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2);

パラメーター

str0
ReadOnlySpan<Char>

連結する最初の読み取り専用文字スパン。

str1
ReadOnlySpan<Char>

連結する 2 番目の読み取り専用文字スパン。

str2
ReadOnlySpan<Char>

連結する 3 番目の読み取り専用文字スパン。

戻り値

str0str1、および str2の値の連結された文字列表現。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat(Object, Object, Object)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定された 3 つのオブジェクトの文字列形式を連結します。

C#
public static string Concat (object arg0, object arg1, object arg2);
C#
public static string Concat (object? arg0, object? arg1, object? arg2);

パラメーター

arg0
Object

連結する最初のオブジェクト。

arg1
Object

連結する 2 番目のオブジェクト。

arg2
Object

連結する 3 番目のオブジェクト。

戻り値

arg0arg1、および arg2の値の連結された文字列表現。

次の例では、Concat メソッドを示します。

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

注釈

メソッドは、各オブジェクトのパラメーターなしの ToString メソッドを呼び出すことによって、arg0arg1、および arg2 を連結します。区切り記号は追加されません。

String.Empty は、null 引数の代わりに使用されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(String, String)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

Stringの 2 つの指定されたインスタンスを連結します。

C#
public static string Concat (string str0, string str1);
C#
public static string Concat (string? str0, string? str1);

パラメーター

str0
String

連結する最初の文字列。

str1
String

連結する 2 番目の文字列。

戻り値

str0str1の連結。

次の例では、ユーザーの名、ミドル ネーム、姓を連結します。

C#
using System;

public class ConcatTest {
    public static void Main() {

        // we want to simply quickly add this person's name together
        string fName = "Simon";
        string mName = "Jake";
        string lName = "Harrows";

        // because we want a name to appear with a space in between each name,
        // put a space on the front of the middle, and last name, allowing for
        // the fact that a space may already be there
        mName = " " + mName.Trim();
        lName = " " + lName.Trim();

        // this line simply concatenates the two strings
        Console.WriteLine("Welcome to this page, '{0}'!", string.Concat( string.Concat(fName, mName), lName ) );
    }
}
// The example displays the following output:
//        Welcome to this page, 'Simon Jake Harrows'!

注釈

メソッドは str0str1を連結します。区切り記号は追加されません。

Empty 文字列は、null 引数の代わりに使用されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(Object)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定したオブジェクトの文字列形式を作成します。

C#
public static string Concat (object arg0);
C#
public static string Concat (object? arg0);

パラメーター

arg0
Object

表すオブジェクト、または nullします。

戻り値

arg0の値の文字列形式。arg0nullされている場合は Empty

次の例では、Concat メソッドを示します。

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

注釈

Concat(Object) メソッドは、パラメーターなしの ToString メソッドを呼び出すことによって、arg0 を文字列として表します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(Object, Object)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定された 2 つのオブジェクトの文字列形式を連結します。

C#
public static string Concat (object arg0, object arg1);
C#
public static string Concat (object? arg0, object? arg1);

パラメーター

arg0
Object

連結する最初のオブジェクト。

arg1
Object

連結する 2 番目のオブジェクト。

戻り値

arg0arg1の値の連結された文字列表現。

次の例では、Concat メソッドを示します。

C#
using System;

class stringConcat5 {
    public static void Main() {
    int i = -123;
    Object o = i;
    Object[] objs = new Object[] {-123, -456, -789};

    Console.WriteLine("Concatenate 1, 2, and 3 objects:");
    Console.WriteLine("1) {0}", String.Concat(o));
    Console.WriteLine("2) {0}", String.Concat(o, o));
    Console.WriteLine("3) {0}", String.Concat(o, o, o));

    Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
    Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
    Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));

    Console.WriteLine("\nConcatenate a 3-element object array:");
    Console.WriteLine("6) {0}", String.Concat(objs));
    }
}
// The example displays the following output:
//    Concatenate 1, 2, and 3 objects:
//    1) -123
//    2) -123-123
//    3) -123-123-123
//
//    Concatenate 4 objects and a variable length parameter list:
//    4) -123-123-123-123
//    5) -123-123-123-123-123
//
//    Concatenate a 3-element object array:
//    6) -123-456-789

注釈

メソッドは、arg0arg1のパラメーターなしの ToString メソッドを呼び出すことによって、arg0arg1 を連結します。区切り記号は追加されません。

String.Empty は、null 引数の代わりに使用されます。

いずれかの引数が配列参照の場合、メソッドはメンバーではなく、その配列を表す文字列を連結します (例: "System.String[]")。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(String[])

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

重要

この API は CLS 準拠ではありません。

指定した String 配列の要素を連結します。

C#
public static string Concat (params string[] values);
C#
public static string Concat (params string?[] values);
C#
[System.CLSCompliant(false)]
public static string Concat (params string[] values);

パラメーター

values
String[]

文字列インスタンスの配列。

戻り値

valuesの連結された要素。

属性

例外

valuesnullです。

メモリ不足。

次の例では、String 配列で Concat メソッドを使用する方法を示します。

C#
using System;

public class Example
{
    public static void Main()
    {
        // Make an array of strings. Note that we have included spaces.
        string [] s = { "hello ", "and ", "welcome ", "to ",
                        "this ", "demo! " };

        // Put all the strings together.
        Console.WriteLine(string.Concat(s));

        // Sort the strings, and put them together.
        Array.Sort(s);
        Console.WriteLine(string.Concat(s));
    }
}
// The example displays the following output:
//       hello and welcome to this demo!
//       and demo! hello this to welcome

注釈

メソッドは、values内の各オブジェクトを連結します。区切り記号は追加されません。

配列内の null オブジェクトの代わりに、Empty 文字列が使用されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(ReadOnlySpan<String>)

Stringの指定したスパンの要素を連結します。

C#
public static string Concat (scoped ReadOnlySpan<string?> values);

パラメーター

values
ReadOnlySpan<String>

String インスタンスのスパン。

戻り値

valuesの連結された要素。

適用対象

.NET 9
製品 バージョン
.NET 9

Concat(ReadOnlySpan<Object>)

オブジェクトの指定されたスパン内の要素の文字列表現を連結します。

C#
public static string Concat (scoped ReadOnlySpan<object?> args);

パラメーター

args
ReadOnlySpan<Object>

連結する要素を含むオブジェクトのスパン。

戻り値

args内の要素の値の連結された文字列表現。

適用対象

.NET 9
製品 バージョン
.NET 9

Concat(Object[])

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定した Object 配列内の要素の文字列形式を連結します。

C#
public static string Concat (params object[] args);
C#
public static string Concat (params object?[] args);

パラメーター

args
Object[]

連結する要素を含むオブジェクト配列。

戻り値

args内の要素の値の連結された文字列表現。

例外

argsnullです。

メモリ不足。

次の例では、Object 配列で Concat メソッドを使用する方法を示します。

C#
using System;

public class ConcatTest {
    public static void Main() {
        // Create a group of objects.
        Test1 t1 = new Test1();
        Test2 t2 = new Test2();
        int i = 16;
        string s = "Demonstration";

        // Place the objects in an array.
        object [] o = { t1, i, t2, s };

        // Concatenate the objects together as a string. To do this,
        // the ToString method of each of the objects is called.
        Console.WriteLine(string.Concat(o));
    }
}

// Create two empty test classes.
class Test1 {
}

class Test2 {
}
// The example displays the following output:
//       Test116Test2Demonstration

注釈

メソッドは、そのオブジェクトのパラメーターなしの ToString メソッドを呼び出すことによって、args 内の各オブジェクトを連結します。区切り記号は追加されません。

String.Empty は、配列内の null オブジェクトの代わりに使用されます。

注意 (呼び出し元)

このメソッドは C++ コードでは呼び出されません。 C++ コンパイラは、Concat(Object, Object, Object, Object)の呼び出しとして 4 つ以上のオブジェクト パラメーターを持つ Concat の呼び出しを解決します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(IEnumerable<String>)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

String型の構築された IEnumerable<T> コレクションのメンバーを連結します。

C#
public static string Concat (System.Collections.Generic.IEnumerable<string> values);
C#
public static string Concat (System.Collections.Generic.IEnumerable<string?> values);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public static string Concat (System.Collections.Generic.IEnumerable<string> values);

パラメーター

values
IEnumerable<String>

IEnumerable<T> を実装し、ジェネリック型引数が Stringされるコレクション オブジェクト。

戻り値

valuesで連結された文字列。または、values が空の IEnumerable(Of String)の場合は Empty

属性

例外

valuesnullです。

次の例では、Eratosthenes アルゴリズムのシーブを使用して、100 以下の素数を計算します。 String型の List<T> オブジェクトに結果が割り当てられ、Concat(IEnumerable<String>) メソッドに渡されます。

C#
using System;
using System.Collections.Generic;

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

   private static IEnumerable<String> GetPrimes(int maxPrime)
   {
      Array values = Array.CreateInstance(typeof(int), 
                              new int[] { maxPrime - 1}, new int[] { 2 }); 
      // Use Sieve of Erathsthenes 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<String> primes = new List<String>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr.ToString() + " ");
      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

注釈

メソッドは、values内の各オブジェクトを連結します。区切り記号は追加されません。 valuesの各メンバー間の区切り記号を指定するには、Join(String, IEnumerable<String>) メソッドを呼び出します。

Empty 文字列は、values内の null 要素の代わりに使用されます。

values が空の IEnumerable(Of String)の場合、メソッドは String.Emptyを返します。 valuesnull場合、メソッドは ArgumentNullException 例外をスローします。

Concat(IEnumerable<String>) は、最初に要素を文字列配列に変換せずに、IEnumerable(Of String) コレクション内の各要素を連結できる便利なメソッドです。 これは、Language-Integrated クエリ (LINQ) クエリ式で特に便利です。 次の例では、アルファベットの大文字または小文字を含む List(Of String) オブジェクトを、特定の文字以上の文字を選択するラムダ式に渡します (この例では "M" です)。 Enumerable.Where メソッドによって返される IEnumerable(Of String) コレクションが Concat(IEnumerable<String>) メソッドに渡され、結果が 1 つの文字列として表示されます。

C#
using System;
using System.Collections.Generic;
using System.Linq;

public class Example
{
   public static void Main()
   {
      string output = String.Concat( GetAlphabet(true).Where( letter => 
                      letter.CompareTo("M") >= 0));
      Console.WriteLine(output);  
   }

   private static List<string> GetAlphabet(bool upper)
   {
      List<string> alphabet = new List<string>();
      int charValue = upper ? 65 : 97;
      for (int ctr = 0; ctr <= 25; ctr++)
         alphabet.Add(((char)(charValue + ctr)).ToString());
      return alphabet; 
   }
}
// The example displays the following output:
//      MNOPQRSTUVWXYZ

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

指定された 2 つの読み取り専用文字スパンの文字列表現を連結します。

C#
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1);

パラメーター

str0
ReadOnlySpan<Char>

連結する最初の読み取り専用文字スパン。

str1
ReadOnlySpan<Char>

連結する 2 番目の読み取り専用文字スパン。

戻り値

str0str1の値の連結された文字列表現。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat<T>(IEnumerable<T>)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs

IEnumerable<T> 実装のメンバーを連結します。

C#
public static string Concat<T> (System.Collections.Generic.IEnumerable<T> values);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public static string Concat<T> (System.Collections.Generic.IEnumerable<T> values);

型パラメーター

T

valuesのメンバーの型。

パラメーター

values
IEnumerable<T>

IEnumerable<T> インターフェイスを実装するコレクション オブジェクト。

戻り値

values内の連結されたメンバー。

属性

例外

valuesnullです。

次の例では、動物の名前とそれが属する順序を含む非常に単純な Animal クラスを定義します。 次に、多数の Animal オブジェクトを格納する List<T> オブジェクトを定義します。 Enumerable.Where 拡張メソッドは、Order プロパティが "Rodent" と等しい Animal オブジェクトを抽出するために呼び出されます。 結果は Concat<T>(IEnumerable<T>) メソッドに渡され、コンソールに表示されます。

C#
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.Concat(animals.Where( animal => 
                      (animal.Order == "Rodent")));
      Console.WriteLine(output);  
   }
}
// The example displays the following output:
//      SquirrelCapybara

注釈

メソッドは、values内の各オブジェクトを連結します。区切り記号は追加されません。

Empty 文字列は、null 引数の代わりに使用されます。

Concat<T>(IEnumerable<T>) は、最初に要素を文字列に変換せずに、IEnumerable<T> コレクション内の各要素を連結できる便利なメソッドです。 この例に示すように、Language-Integrated クエリ (LINQ) クエリ式では特に便利です。 IEnumerable<T> コレクション内の各オブジェクトの文字列形式は、そのオブジェクトの ToString メソッドを呼び出すことによって派生します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0