String.Concat 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
Concat(IEnumerable<String>) |
串連類型 IEnumerable<T> 之已建構的 String 集合的成員。 |
Concat(String, String, String, String) |
串連 String 的四個指定執行個體。 |
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
串連四個指定唯讀字元範圍的字串表示。 |
Concat(String, String, String) |
串連 String 的三個指定執行個體。 |
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
串連三個指定唯讀字元範圍的字串表示。 |
Concat(Object, Object, Object) |
串連三個指定之物件的字串表示。 |
Concat(Object, Object, Object, Object) |
串連四個指定的物件之字串表示和選擇性變數長度參數清單中所指定的任何物件。 |
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
串連兩個指定唯讀字元範圍的字串表示。 |
Concat(Object, Object) |
串連兩個指定之物件的字串表示。 |
Concat(String[]) |
串連指定 String 陣列中的項目。 |
Concat(Object[]) |
串連指定之 Object 陣列中項目的字串表示法。 |
Concat(Object) |
建立指定之物件的字串表示。 |
Concat(String, String) |
串連 String 的兩個指定執行個體。 |
Concat<T>(IEnumerable<T>) |
串連 IEnumerable<T> 實作的成員。 |
備註
注意
您也可以使用語言的字串串連運算子(例如 +
在 c # 中),或使用 &
+
Visual Basic 中的來串連字號串。 這兩個編譯器都會將串連運算子轉譯成的其中一個多載的呼叫 String.Concat
。
Concat(IEnumerable<String>)
串連類型 IEnumerable<T> 之已建構的 String 集合的成員。
public:
static System::String ^ Concat(System::Collections::Generic::IEnumerable<System::String ^> ^ values);
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);
static member Concat : seq<string> -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member Concat : seq<string> -> string
Public Shared Function Concat (values As IEnumerable(Of String)) As String
參數
- values
- IEnumerable<String>
集合物件,這個物件實作 IEnumerable<T>,且其泛型類型引數為 String。
傳回
values
中的串連字串,或如果 values
是空白的 IEnumerable(Of String)
則為 Empty。
- 屬性
例外狀況
values
為 null
。
範例
下列範例會使用 Eratosthenes 演算法的 Sieve 來計算小於或等於100的質數。 它會將結果指派給 List<T> 型別的物件 String ,然後傳遞給 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
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim maxPrime As Integer = 100
Dim primeList As IEnumerable(Of String) = GetPrimes(maxPrime)
Console.WriteLine("Primes less than {0}:", maxPrime)
Console.WriteLine(" {0}", String.Concat(primeList))
End Sub
Private Function GetPrimes(maxPrime As Integer) As IEnumerable(Of String)
Dim values As Array = Array.CreateInstance(GetType(Integer), _
New Integer() { maxPrime - 1}, New Integer(){ 2 })
' Use Sieve of Erathsthenes to determine prime numbers.
For ctr As Integer = values.GetLowerBound(0) To _
CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))))
If CInt(values.GetValue(ctr)) = 1 Then Continue For
For multiplier As Integer = ctr To maxPrime \ 2
If ctr * multiplier <= maxPrime Then values.SetValue(1, ctr * multiplier)
Next
Next
Dim primes As New List(Of String)
For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr.ToString() + " ")
Next
Return primes
End Function
End Module
' 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字串會用來取代中的任何 null 元素 values
。
如果 values
則為 IEnumerable(Of String)
,則方法會傳回 String.Empty。 如果 values
為 null
,則方法會擲回 ArgumentNullException 例外狀況。
Concat(IEnumerable<String>) 是便利的方法,可讓您串連集合中的每個元素, IEnumerable(Of String)
而不需要先將元素轉換成字串陣列。 這特別適用于 Language-Integrated 查詢 (LINQ) 查詢運算式。 下列範例 List(Of String)
會將包含字母的大寫字母或小寫字母的物件,傳遞至 lambda 運算式,以選取等於或大於特定字母的字母 (在此範例中為 "M" ) 。 IEnumerable(Of String)
方法所傳回的集合 Enumerable.Where 會傳遞至方法,以將 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
Imports System.Collections.Generic
Imports System.Linq
Module modMain
Public Sub Main()
Dim output As String = String.Concat(GetAlphabet(true).Where(Function(letter) _
letter >= "M"))
Console.WriteLine(output)
End Sub
Private Function GetAlphabet(upper As Boolean) As List(Of String)
Dim alphabet As New List(Of String)
Dim charValue As Integer = CInt(IIf(upper, 65, 97))
For ctr As Integer = 0 To 25
alphabet.Add(ChrW(charValue + ctr).ToString())
Next
Return alphabet
End Function
End Module
' The example displays the following output:
' MNOPQRSTUVWXYZ
適用於
Concat(String, String, String, String)
串連 String 的四個指定執行個體。
public:
static System::String ^ Concat(System::String ^ str0, System::String ^ str1, System::String ^ str2, System::String ^ str3);
public static string Concat (string str0, string str1, string str2, string str3);
public static string Concat (string? str0, string? str1, string? str2, string? str3);
static member Concat : string * string * string * string -> string
Public Shared Function Concat (str0 As String, str1 As String, str2 As String, str3 As String) As String
參數
- str0
- String
要串連的第一個字串。
- str1
- String
要串連的第二個字串。
- str2
- String
要串連的第三個字串。
- str3
- String
要串連的第四個字串。
傳回
str0
、str1
、str2
和 str3
的串連。
範例
下列範例會定義四個字母的單字陣列,並將其個別字母儲存至字串陣列以進行編碼。 然後,它會呼叫方法來重新組合未 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
Imports System.Collections
Module Example
Public Sub Main()
Const WORD_SIZE As Integer = 4
' Define some 4-letter words to be scrambled.
Dim words() As String = { "home", "food", "game", "rest" }
' Define two arrays equal to the number of letters in each word.
Dim keys(WORD_SIZE) As Double
Dim letters(WORD_SIZE) As String
' Initialize the random number generator.
Dim rnd As New Random()
' Scramble each word.
For Each word As String In words
For ctr As Integer = 0 To word.Length - 1
' Populate the array of keys with random numbers.
keys(ctr) = rnd.NextDouble()
' Assign a letter to the array of letters.
letters(ctr) = word.Chars(ctr)
Next
' Sort the array.
Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default)
' Display the scrambled word.
Dim scrambledWord As String = String.Concat(letters(0), letters(1), _
letters(2), letters(3))
Console.WriteLine("{0} --> {1}", word, scrambledWord)
Next
End Sub
End Module
' The example displays output like the following:
' home --> mheo
' food --> oodf
' game --> aemg
' rest --> trse
備註
方法會串連 str0
、 str1
、 str2
和 str3
; 它不會加入任何分隔符號。
另請參閱
適用於
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)
串連四個指定唯讀字元範圍的字串表示。
public:
static System::String ^ Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2, ReadOnlySpan<char> str3);
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2, ReadOnlySpan<char> str3);
static member Concat : ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Concat (str0 As ReadOnlySpan(Of Char), str1 As ReadOnlySpan(Of Char), str2 As ReadOnlySpan(Of Char), str3 As ReadOnlySpan(Of Char)) As String
參數
- str0
- ReadOnlySpan<Char>
要串連的第一個唯讀字元範圍。
- str1
- ReadOnlySpan<Char>
要串連的第二個唯讀字元範圍。
- str2
- ReadOnlySpan<Char>
要串連的第三個唯讀字元範圍。
- str3
- ReadOnlySpan<Char>
要串連的第四個唯讀字元範圍。
傳回
str0
、str1
、str2
和 str3
的值串連字串表示。
適用於
Concat(String, String, String)
串連 String 的三個指定執行個體。
public:
static System::String ^ Concat(System::String ^ str0, System::String ^ str1, System::String ^ str2);
public static string Concat (string str0, string str1, string str2);
public static string Concat (string? str0, string? str1, string? str2);
static member Concat : string * string * string -> string
Public Shared Function Concat (str0 As String, str1 As String, str2 As String) As String
參數
- str0
- String
要串連的第一個字串。
- str1
- String
要串連的第二個字串。
- str2
- String
要串連的第三個字串。
傳回
str0
、str1
和 str2
的串連。
範例
下列範例會使用 Concat 方法來串連三個字串並顯示結果。
using namespace System;
void main()
{
String^ s1 = "We went to a bookstore, ";
String^ s2 = "a movie, ";
String^ s3 = "and a restaurant.";
String^ 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.
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.
Public Module Example
Public Sub Main()
Dim s1 As String = "We went to a bookstore, "
Dim s2 As String = "a movie, "
Dim s3 As String = "and a restaurant."
Dim s = String.Concat(s1, s2, s3)
Console.WriteLine(s)
End Sub
End Module
' The example displays the following output:
' We went to a bookstore, a movie, and a restaurant.
備註
方法會串連 str0
、 str1
和 str2
; 它不會加入任何分隔符號。
另請參閱
適用於
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)
串連三個指定唯讀字元範圍的字串表示。
public:
static System::String ^ Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2);
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2);
static member Concat : ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Concat (str0 As ReadOnlySpan(Of Char), str1 As ReadOnlySpan(Of Char), str2 As ReadOnlySpan(Of Char)) As String
參數
- str0
- ReadOnlySpan<Char>
要串連的第一個唯讀字元範圍。
- str1
- ReadOnlySpan<Char>
要串連的第二個唯讀字元範圍。
- str2
- ReadOnlySpan<Char>
要串連的第三個唯讀字元範圍。
傳回
str0
、str1
和 str2
值的串連字串表示。
適用於
Concat(Object, Object, Object)
串連三個指定之物件的字串表示。
public:
static System::String ^ Concat(System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static string Concat (object arg0, object arg1, object arg2);
public static string Concat (object? arg0, object? arg1, object? arg2);
static member Concat : obj * obj * obj -> string
Public Shared Function Concat (arg0 As Object, arg1 As Object, arg2 As Object) As String
參數
- arg0
- Object
要串連的第一個物件。
- arg1
- Object
要串連的第二個物件。
- arg2
- Object
要串連的第三個物件。
傳回
arg0
、arg1
和 arg2
之值的串連字串表示。
範例
下列範例會示範 Concat 方法。
using namespace System;
int main()
{
int i = -123;
Object^ o = i;
array<Object^>^objs = { -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
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
Class stringConcat5
Public Shared Sub Main()
Dim i As Integer = - 123
Dim o As [Object] = i
Dim objs() As [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(vbCrLf & "Concatenate 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(vbCrLf & "Concatenate a 3-element object array:")
Console.WriteLine("6) {0}", [String].Concat(objs))
End Sub
End Class
'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
備註
方法會透過 arg0
arg1
arg2
呼叫每個物件的無參數方法來串連、和,而不 ToString
會加入任何分隔符號。
String.Empty 用來取代任何 null 引數。
另請參閱
適用於
Concat(Object, Object, Object, Object)
重要
此 API 不符合 CLS 規範。
串連四個指定的物件之字串表示和選擇性變數長度參數清單中所指定的任何物件。
public:
static System::String ^ Concat(System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2, System::Object ^ arg3);
[System.CLSCompliant(false)]
public static string Concat (object arg0, object arg1, object arg2, object arg3);
[<System.CLSCompliant(false)>]
static member Concat : obj * obj * obj * obj -> string
Public Shared Function Concat (arg0 As Object, arg1 As Object, arg2 As Object, arg3 As Object) As String
參數
- 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
Imports System.Collections
Module Example
Public Sub Main()
Const WORD_SIZE As Integer = 4
' Define some 4-letter words to be scrambled.
Dim words() As String = { "home", "food", "game", "rest" }
' Define two arrays equal to the number of letters in each word.
Dim keys(WORD_SIZE) As Double
Dim letters(WORD_SIZE) As String
' Initialize the random number generator.
Dim rnd As New Random()
' Scramble each word.
For Each word As String In words
For ctr As Integer = 0 To word.Length - 1
' Populate the array of keys with random numbers.
keys(ctr) = rnd.NextDouble()
' Assign a letter to the array of letters.
letters(ctr) = word.Chars(ctr)
Next
' Sort the array.
Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default)
' Display the scrambled word.
Dim scrambledWord As String = String.Concat(letters(0), letters(1), _
letters(2), letters(3))
Console.WriteLine("{0} --> {1}", word, scrambledWord)
Next
End Sub
End Module
' The example displays output like the following:
' home --> mheo
' food --> oodf
' game --> aemg
' rest --> trse
備註
注意
此應用程式開發介面不符合 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[]) 。
適用於
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)
串連兩個指定唯讀字元範圍的字串表示。
public:
static System::String ^ Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1);
public static string Concat (ReadOnlySpan<char> str0, ReadOnlySpan<char> str1);
static member Concat : ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Concat (str0 As ReadOnlySpan(Of Char), str1 As ReadOnlySpan(Of Char)) As String
參數
- str0
- ReadOnlySpan<Char>
要串連的第一個唯讀字元範圍。
- str1
- ReadOnlySpan<Char>
要串連的第二個唯讀字元範圍。
傳回
str0
和 str1
之值的串連字串表示。
適用於
Concat(Object, Object)
串連兩個指定之物件的字串表示。
public:
static System::String ^ Concat(System::Object ^ arg0, System::Object ^ arg1);
public static string Concat (object arg0, object arg1);
public static string Concat (object? arg0, object? arg1);
static member Concat : obj * obj -> string
Public Shared Function Concat (arg0 As Object, arg1 As Object) As String
參數
- arg0
- Object
要串連的第一個物件。
- arg1
- Object
要串連的第二個物件。
傳回
arg0
和 arg1
之值的串連字串表示。
範例
下列範例會示範 Concat 方法。
using namespace System;
int main()
{
int i = -123;
Object^ o = i;
array<Object^>^objs = { -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
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
Class stringConcat5
Public Shared Sub Main()
Dim i As Integer = - 123
Dim o As [Object] = i
Dim objs() As [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(vbCrLf & "Concatenate 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(vbCrLf & "Concatenate a 3-element object array:")
Console.WriteLine("6) {0}", [String].Concat(objs))
End Sub
End Class
'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
備註
方法會串連和,方法是 arg0
arg1
呼叫和的無參數方法,不 ToString
arg0
arg1
會新增任何分隔符號。
String.Empty 用來取代任何 null 引數。
如果任一個引數是陣列參考,則方法會串連代表該陣列的字串,而不是其成員 (例如 "System.string []" ) 。
另請參閱
適用於
Concat(String[])
重要
此 API 不符合 CLS 規範。
串連指定 String 陣列中的項目。
public:
static System::String ^ Concat(... cli::array <System::String ^> ^ values);
public static string Concat (params string[] values);
public static string Concat (params string?[] values);
[System.CLSCompliant(false)]
public static string Concat (params string[] values);
static member Concat : string[] -> string
[<System.CLSCompliant(false)>]
static member Concat : string[] -> string
Public Shared Function Concat (ParamArray values As String()) As String
參數
- values
- String[]
字串執行個體的陣列。
傳回
values
的串連項目。
- 屬性
例外狀況
values
為 null
。
記憶體不足。
範例
下列範例示範如何搭配使用 Concat 方法與 String 陣列。
using namespace System;
int main()
{
// Make an array of strings. Note that we have included spaces.
array<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
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
Public Class Example
Public Shared Sub Main()
' Make an array of strings. Note that we have included spaces.
Dim s As String() = { "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))
End Sub
End Class
' The example displays the following output:
' hello and welcome to this demo!
' and demo! hello this to welcome
備註
方法會串連中的每個物件,而不 values
會加入任何分隔符號。
Empty字串會用來取代陣列中的任何 null 物件。
另請參閱
適用於
Concat(Object[])
串連指定之 Object 陣列中項目的字串表示法。
public:
static System::String ^ Concat(... cli::array <System::Object ^> ^ args);
public static string Concat (params object[] args);
public static string Concat (params object?[] args);
static member Concat : obj[] -> string
Public Shared Function Concat (ParamArray args As Object()) As String
參數
- args
- Object[]
包含要串連之項目的物件陣列。
傳回
args
中之項目值的串連字串表示。
例外狀況
args
為 null
。
記憶體不足。
範例
下列範例示範如何搭配使用 Concat 方法與 Object 陣列。
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
Public Class ConcatTest
Public Shared Sub Main()
Dim t1 As New Test1()
Dim t2 As New Test2()
Dim i As Integer = 16
Dim s As String = "Demonstration"
Dim o As Object() = {t1, i, t2, s}
' create a group of objects
' place the objects in an array
' concatenate the objects together as a string. To do this,
' the ToString method in the objects is called
Console.WriteLine(String.Concat(o))
End Sub
End Class
' imagine these test classes are full-fledged objects...
Class Test1
End Class
Class Test2
End Class
備註
方法 args
會藉由呼叫該物件的無參數方法來串連中的每個物件,而不 ToString
會加入任何分隔符號。
String.Empty 用來取代陣列中的任何 null 物件。
給呼叫者的注意事項
C + + 程式碼不會呼叫這個方法。 C + + 編譯器會解析 Concat 具有四個或更多物件參數的呼叫,以做為的呼叫 Concat(Object, Object, Object, Object) 。
另請參閱
適用於
Concat(Object)
建立指定之物件的字串表示。
public:
static System::String ^ Concat(System::Object ^ arg0);
public static string Concat (object arg0);
public static string Concat (object? arg0);
static member Concat : obj -> string
Public Shared Function Concat (arg0 As Object) As String
參數
- arg0
- Object
要表示的物件,或 null
。
傳回
arg0
值的字串表示;如果 Empty 為 arg0
,則為 null
。
範例
下列範例會示範 Concat 方法。
using namespace System;
int main()
{
int i = -123;
Object^ o = i;
array<Object^>^objs = { -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
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
Class stringConcat5
Public Shared Sub Main()
Dim i As Integer = - 123
Dim o As [Object] = i
Dim objs() As [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(vbCrLf & "Concatenate 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(vbCrLf & "Concatenate a 3-element object array:")
Console.WriteLine("6) {0}", [String].Concat(objs))
End Sub
End Class
'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)方法會藉 arg0
由呼叫其無參數方法來表示為字串 ToString
。
另請參閱
適用於
Concat(String, String)
串連 String 的兩個指定執行個體。
public:
static System::String ^ Concat(System::String ^ str0, System::String ^ str1);
public static string Concat (string str0, string str1);
public static string Concat (string? str0, string? str1);
static member Concat : string * string -> string
Public Shared Function Concat (str0 As String, str1 As String) As String
參數
- str0
- String
要串連的第一個字串。
- str1
- String
要串連的第二個字串。
傳回
str0
和 str1
的串連。
範例
下列範例會串連 person 的名字、中間名和姓氏。
using namespace System;
int 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 = String::Concat( " ", mName->Trim() );
lName = String::Concat( " ", 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'!
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'!
Public Class ConcatTest
Public Shared Sub Main()
Dim fName As String = "Simon"
Dim mName As String = "Jake"
Dim lName As String = "Harrows"
' We want to simply quickly add this person's name together.
' Because we want a name to appear with a space in between each name,
' we 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))
End Sub
End Class
' The example displays the following output:
' Welcome to this page, 'Simon Jake Harrows'!
備註
方法會串連 str0
和,而不 str1
會加入任何分隔符號。
Empty用來取代任何 null 引數的字串。
另請參閱
適用於
Concat<T>(IEnumerable<T>)
串連 IEnumerable<T> 實作的成員。
public:
generic <typename T>
static System::String ^ Concat(System::Collections::Generic::IEnumerable<T> ^ values);
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);
static member Concat : seq<'T> -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member Concat : seq<'T> -> string
Public Shared Function Concat(Of T) (values As IEnumerable(Of T)) As String
類型參數
- T
values
之成員的類型。
參數
- values
- IEnumerable<T>
集合物件,這個物件實作 IEnumerable<T> 介面。
傳回
values
中的串連成員。
- 屬性
例外狀況
values
為 null
。
範例
下列範例會定義非常簡單的 Animal
類別,其中包含動物的名稱及其所屬的順序。 接著,它會定義一個 List<T> 物件來包含多個 Animal
物件。 Enumerable.Where會呼叫擴充方法,將 Animal
其 Order
屬性等於 "齧齒動物" 的物件解壓縮。 結果會傳遞至 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
Imports System.Collections.Generic
Public Class Animal
Public Kind As String
Public Order As String
Public Sub New(kind As String, order As String)
Me.Kind = kind
Me.Order = order
End Sub
Public Overrides Function ToString() As String
Return Me.Kind
End Function
End Class
Module Example
Public Sub Main()
Dim animals As New List(Of Animal)
animals.Add(New Animal("Squirrel", "Rodent"))
animals.Add(New Animal("Gray Wolf", "Carnivora"))
animals.Add(New Animal("Capybara", "Rodent"))
Dim output As String = String.Concat(animals.Where(Function(animal) _
animal.Order = "Rodent"))
Console.WriteLine(output)
End Sub
End Module
' The example displays the following output:
' SquirrelCapybara
備註
方法會串連中的每個物件,而不 values
會加入任何分隔符號。
Empty用來取代任何 null 引數的字串。
Concat<T>(IEnumerable<T>) 是便利的方法,可讓您串連集合中的每個元素, IEnumerable<T> 而不需要先將元素轉換成字串。 這特別適用于 Language-Integrated Query (LINQ) 查詢運算式,如範例所示。 集合中每個物件的字串表示 IEnumerable<T> 都是藉由呼叫該物件的 ToString
方法所衍生。