閱讀英文

共用方式為


String.Concat 方法

定義

串連一或多個 String實例,或一或多個 Object實例之值的 String 表示。

多載

Concat(String, String, String, String)

串連 String的四個指定實例。

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

串連四個指定唯讀字元範圍的字串表示。

Concat(Object, Object, Object, Object)

串連四個指定物件的字串表示,以及選擇性可變長度參數清單中指定的任何物件。

Concat(String, String, String)

串連三個指定的實體 String

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

串連三個指定唯讀字元範圍的字串表示。

Concat(Object, Object, Object)

串連三個指定物件的字串表示。

Concat(String, String)

串連兩個指定的實體 String

Concat(Object)

建立指定物件的字串表示。

Concat(Object, Object)

串連兩個指定物件的字串表示。

Concat(String[])

串連指定之 String 陣列的專案。

Concat(ReadOnlySpan<String>)

串連指定範圍 String的專案。

Concat(ReadOnlySpan<Object>)

串連指定之物件範圍中元素的字串表示。

Concat(Object[])

串連指定之 Object 陣列中元素的字串表示。

Concat(IEnumerable<String>)

串連類型 String之建構 IEnumerable<T> 集合的成員。

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

串連兩個指定唯讀字元範圍的字串表示。

Concat<T>(IEnumerable<T>)

串連 IEnumerable<T> 實作的成員。

備註

注意

您也可以使用語言的字串串連運算符,例如 C# 和 F# 中的 +,或在 Visual Basic 中使用 &+ 串連字串串。 這兩個編譯程式會將串連運算符轉譯成對其中一個 多載的呼叫,String.Concat

Concat(String, String, String, String)

來源:
String.Manipulation.cs
來源:
String.Manipulation.cs
來源:
String.Manipulation.cs

串連 String的四個指定實例。

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

參數

str0
String

要串連的第一個字串。

str1
String

要串連的第二個字串。

str2
String

要串連的第三個字串。

str3
String

要串連的第四個字串。

傳回

str0str1str2str3的串連。

範例

下列範例會定義四個字母字組的陣列,並將其個別字母儲存至字串陣列,以便加以拼字。 然後,它會呼叫 Concat(String, String, String, String) 方法來重新組合拼字。

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

備註

方法會串連 str0str1str2str3;它不會新增任何分隔符。

另請參閱

適用於

.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

串連四個指定唯讀字元範圍的字串表示。

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

參數

str0
ReadOnlySpan<Char>

要串連的第一個唯讀字元範圍。

str1
ReadOnlySpan<Char>

要串連的第二個唯讀字元範圍。

str2
ReadOnlySpan<Char>

要串連的第三個唯讀字元範圍。

str3
ReadOnlySpan<Char>

要串連的第四個唯讀字元範圍。

傳回

str0str1str2str3值的串連字串表示。

適用於

.NET 9 及其他版本
產品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

Concat(Object, Object, Object, Object)

重要

此 API 不符合 CLS 規範。

串連四個指定物件的字串表示,以及選擇性可變長度參數清單中指定的任何物件。

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

參數

arg0
Object

要串連的第一個物件。

arg1
Object

要串連的第二個物件。

arg2
Object

要串連的第三個物件。

arg3
Object

要串連的第四個物件。

傳回

參數清單中每個值的串連字串表示。

屬性

範例

下列範例說明如何使用 Concat(Object, Object, Object, Object) 方法來串連變數參數清單。 在此情況下,會使用九個參數呼叫 方法。

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 方法的最後一個參數是一或多個要串連之其他對象的選擇性逗號分隔清單。

給呼叫者的注意事項

這個方法會以 vararg 關鍵詞標示,這表示它支援變數數目的參數。 方法可以從 Visual C++呼叫,但無法從 C# 或 Visual Basic 程式代碼呼叫。 C# 和 Visual Basic 編譯程式會將對 Concat(Object, Object, Object, Object) 的呼叫解析為對 Concat(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

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

參數

str0
String

要串連的第一個字串。

str1
String

要串連的第二個字串。

str2
String

要串連的第三個字串。

傳回

str0str1str2的串連。

範例

下列範例會使用 Concat 方法來串連三個字串並顯示結果。

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.

備註

方法會串連 str0str1str2;它不會新增任何分隔符。

另請參閱

適用於

.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

串連三個指定唯讀字元範圍的字串表示。

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

參數

str0
ReadOnlySpan<Char>

要串連的第一個唯讀字元範圍。

str1
ReadOnlySpan<Char>

要串連的第二個唯讀字元範圍。

str2
ReadOnlySpan<Char>

要串連的第三個唯讀字元範圍。

傳回

str0str1str2值的串連字串表示。

適用於

.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

串連三個指定物件的字串表示。

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

參數

arg0
Object

要串連的第一個物件。

arg1
Object

要串連的第二個物件。

arg2
Object

要串連的第三個物件。

傳回

arg0arg1arg2值的串連字串表示。

範例

下列範例示範 Concat 方法。

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 方法,以串連 arg0arg1arg2:它不會新增任何分隔符。

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

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

參數

str0
String

要串連的第一個字串。

str1
String

要串連的第二個字串。

傳回

str0str1的串連。

範例

下列範例會串連人員的第一個、中間和姓氏。

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

建立指定物件的字串表示。

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

參數

arg0
Object

要表示的物件,或 null

傳回

arg0值的字串表示,如果 arg0null,則為 Empty

範例

下列範例示範 Concat 方法。

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

串連兩個指定物件的字串表示。

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

參數

arg0
Object

要串連的第一個物件。

arg1
Object

要串連的第二個物件。

傳回

arg0arg1值的串連字串表示。

範例

下列範例示範 Concat 方法。

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方法串連 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 陣列的專案。

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

參數

values
String[]

字串實例的陣列。

傳回

values的串連專案。

屬性

例外狀況

values null

記憶體不足。

範例

下列範例示範搭配 String 陣列使用 Concat 方法。

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中的每個物件;它不會新增任何分隔符。

會使用 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(ReadOnlySpan<String>)

串連指定範圍 String的專案。

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

參數

values
ReadOnlySpan<String>

String 實例範圍。

傳回

values的串連專案。

適用於

.NET 9
產品 版本
.NET 9

Concat(ReadOnlySpan<Object>)

串連指定之物件範圍中元素的字串表示。

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 陣列中元素的字串表示。

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

參數

args
Object[]

對象陣列,其中包含要串連的專案。

傳回

args中專案值的串連字串表示。

例外狀況

args null

記憶體不足。

範例

下列範例示範搭配 Object 陣列使用 Concat 方法。

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 的呼叫,做為對 Concat(Object, Object, Object, Object)的呼叫。

另請參閱

適用於

.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> 集合的成員。

public static string Concat (System.Collections.Generic.IEnumerable<string> values);
public static string Concat (System.Collections.Generic.IEnumerable<string?> values);
[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

屬性

例外狀況

values null

範例

下列範例會使用 Eratosthenes 演算法的 Sieve 來計算小於或等於 100 的質數。 它會將結果指派給類型為 StringList<T> 對象,然後傳遞給 Concat(IEnumerable<String>) 方法。

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 Query (LINQ) 查詢表達式。 下列範例會將包含字母大寫或小寫字母的 List(Of String) 對象傳遞至 Lambda 運算式,該表達式會選取等於或大於特定字母的字母(在此範例中為 “M” )。 Enumerable.Where 方法傳回的 IEnumerable(Of String) 集合會傳遞至 Concat(IEnumerable<String>) 方法,以將結果顯示為單一字串。

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

串連兩個指定唯讀字元範圍的字串表示。

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

參數

str0
ReadOnlySpan<Char>

要串連的第一個唯讀字元範圍。

str1
ReadOnlySpan<Char>

要串連的第二個唯讀字元範圍。

傳回

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> 實作的成員。

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

類型參數

T

values的成員類型。

參數

values
IEnumerable<T>

實作 IEnumerable<T> 介面的集合物件。

傳回

values中的串連成員。

屬性

例外狀況

values null

範例

下列範例會定義非常簡單的 Animal 類別,其中包含動物的名稱及其所屬的順序。 然後,它會定義 List<T> 物件,以包含一些 Animal 物件。 呼叫 Enumerable.Where 擴充方法,以擷取 Order 屬性等於 “Rodent” 的 Animal 物件。 結果會傳遞至 Concat<T>(IEnumerable<T>) 方法,並顯示至主控台。

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 Query (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