String.Concat 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
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)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接四个指定的 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
open System
open System.Collections
let WORD_SIZE = 4
// Define some 4-letter words to be scrambled.
let words = [| "home"; "food"; "game"; "rest" |]
// Define two arrays equal to the number of letters in each word.
let keys = Array.zeroCreate<float> WORD_SIZE
let letters = Array.zeroCreate<string> WORD_SIZE
// Initialize the random number generator.
let rnd = Random()
// Scramble each word.
for word in words do
for i = 0 to word.Length - 1 do
// Populate the array of keys with random numbers.
keys[i] <- rnd.NextDouble()
// Assign a letter to the array of letters.
letters[i] <- string word[i]
// Sort the array.
Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default)
// Display the scrambled word.
let scrambledWord = String.Concat(letters[0], letters[1], letters[2], letters[3])
printfn $"{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>)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接四个指定的只读字符范围的字符串表示形式。
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(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
open System
open System.Collections
let WORD_SIZE = 4
// Define some 4-letter words to be scrambled.
let words = [| "home"; "food"; "game"; "rest" |]
// Define two arrays equal to the number of letters in each word.
let keys = Array.zeroCreate<float> WORD_SIZE
let letters = Array.zeroCreate<string> WORD_SIZE
// Initialize the random number generator.
let rnd = Random()
// Scramble each word.
for word in words do
for i = 0 to word.Length - 1 do
// Populate the array of keys with random numbers.
keys[i] <- rnd.NextDouble()
// Assign a letter to the array of letters.
letters[i] <- string word[i]
// Sort the array.
Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default)
// Display the scrambled word.
let scrambledWord = String.Concat(letters[0], letters[1], letters[2], letters[3])
printfn $"{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
注解
注意
此 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[])的调用。
适用于
Concat(String, String, String)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接三个指定的 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.
open System
let s1 = "We went to a bookstore, "
let s2 = "a movie, "
let s3 = "and a restaurant."
String.Concat(s1, s2, s3)
|> printfn "%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>)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接三个指定的只读字符范围的字符串表示形式。
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)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接三个指定对象的字符串表示形式。
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
open System
let i = -123
let o: obj = i
let objs: obj[] = [| -123; -456; -789 |]
printfn "Concatenate 1, 2, and 3 objects:"
printfn $"1) {String.Concat o}"
printfn $"2) {String.Concat(o, o)}"
printfn $"3) {String.Concat(o, o, o)}"
printfn "\nConcatenate 4 objects and a variable length parameter list:"
printfn $"4) {String.Concat(o, o, o, o)}"
printfn $"5) {String.Concat(o, o, o, o, o)}"
printfn "\nConcatenate a 3-element object array:"
printfn $"6) {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
注解
该方法通过调用每个对象的无参数 ToString
方法连接 arg0
、arg1
和 arg2
;它不添加任何分隔符。
String.Empty 用于代替任何 null 参数。
另请参阅
适用于
Concat(String, String)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接两个指定的 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
的串联。
示例
以下示例连接某人的第一个、中间和姓氏。
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'!
open System
[<EntryPoint>]
let main _ =
// we want to simply quickly add this person's name together
let fName = "Simon"
let mName = "Jake"
let 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
let mName = " " + mName.Trim()
let lName = " " + lName.Trim()
// this line simply concatenates the two strings
printfn $"Welcome to this page, '{String.Concat(String.Concat(fName, mName), lName)}'!"
0
// 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(Object)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
创建指定对象的字符串表示形式。
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
值的字符串表示形式;如果 arg0
null
,则为 Empty。
示例
以下示例演示 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
open System
let i = -123
let o: obj = i
let objs: obj[] = [| -123; -456; -789 |]
printfn "Concatenate 1, 2, and 3 objects:"
printfn $"1) {String.Concat o}"
printfn $"2) {String.Concat(o, o)}"
printfn $"3) {String.Concat(o, o, o)}"
printfn "\nConcatenate 4 objects and a variable length parameter list:"
printfn $"4) {String.Concat(o, o, o, o)}"
printfn $"5) {String.Concat(o, o, o, o, o)}"
printfn "\nConcatenate a 3-element object array:"
printfn $"6) {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) 方法通过调用无参数 ToString
方法将 arg0
表示为字符串。
另请参阅
适用于
Concat(Object, Object)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接两个指定对象的字符串表示形式。
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
open System
let i = -123
let o: obj = i
let objs: obj[] = [| -123; -456; -789 |]
printfn "Concatenate 1, 2, and 3 objects:"
printfn $"1) {String.Concat o}"
printfn $"2) {String.Concat(o, o)}"
printfn $"3) {String.Concat(o, o, o)}"
printfn "\nConcatenate 4 objects and a variable length parameter list:"
printfn $"4) {String.Concat(o, o, o, o)}"
printfn $"5) {String.Concat(o, o, o, o, o)}"
printfn "\nConcatenate a 3-element object array:"
printfn $"6) {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[])
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
重要
此 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
open System
// Make an array of strings. Note that we have included spaces.
let s =
[| "hello "; "and "; "welcome "; "to "
"this "; "demo! " |]
// Put all the strings together.
printfn $"{String.Concat s}"
// Sort the strings, and put them together.
Array.Sort s
printfn $"{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(ReadOnlySpan<String>)
连接指定范围 String的元素。
public:
static System::String ^ Concat(ReadOnlySpan<System::String ^> values);
public static string Concat (scoped ReadOnlySpan<string?> values);
static member Concat : ReadOnlySpan<string> -> string
Public Shared Function Concat (values As ReadOnlySpan(Of String)) As String
参数
- values
- ReadOnlySpan<String>
String 实例的跨度。
返回
values
的串联元素。
适用于
Concat(ReadOnlySpan<Object>)
连接指定对象范围中元素的字符串表示形式。
public:
static System::String ^ Concat(ReadOnlySpan<System::Object ^> args);
public static string Concat (scoped ReadOnlySpan<object?> args);
static member Concat : ReadOnlySpan<obj> -> string
Public Shared Function Concat (args As ReadOnlySpan(Of Object)) As String
参数
- args
- ReadOnlySpan<Object>
包含要连接的元素的对象范围。
返回
args
中元素值的串联字符串表示形式。
适用于
Concat(Object[])
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接指定 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
open System
// Create two empty test classes.
type Test1() = class end
type Test2() = class end
// Create a group of objects.
let t1 = new Test1()
let t2 = new Test2()
let i = 16
let s = "Demonstration"
// Place the objects in an array.
let o: obj[] = [| t1; i; t2; s |]
// Concatenate the objects together as a string. To do this,
// the ToString method of each of the objects is called.
printfn $"{String.Concat o}"
// 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
注解
该方法通过调用该对象的无参数 ToString
方法连接 args
中的每个对象;它不添加任何分隔符。
String.Empty 用于代替数组中的任何 null 对象。
调用方说明
C++代码不调用此方法。 C++编译器解析对 Concat 的调用,这些调用具有四个或更多个对象参数作为对 Concat(Object, Object, Object, Object)的调用。
另请参阅
适用于
Concat(IEnumerable<String>)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接类型为 String的构造 IEnumerable<T> 集合的成员。
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 的质数。 它将结果分配给类型为 String的 List<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
open System
let getPrimes maxPrime =
let values = Array.CreateInstance(typeof<int>, [| maxPrime - 1|], [| 2 |])
// Use Sieve of Erathsthenes to determine prime numbers.
for i = values.GetLowerBound 0 to values.GetUpperBound 0 |> float |> sqrt |> ceil |> int do
if values.GetValue i :?> int <> 1 then
for multiplier = i to maxPrime / 2 do
if i * multiplier <= maxPrime then
values.SetValue(1, i * multiplier)
seq {
for i = values.GetLowerBound 0 to values.GetUpperBound 0 do
if values.GetValue i :?> int = 0 then
string i + " "
}
let maxPrime = 100
let primeList = getPrimes maxPrime
printfn $"Primes less than {maxPrime}:"
printfn $" {String.Concat primeList}"
// 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 字符串用于代替 values
中的任何 null 元素。
如果 values
为空 IEnumerable(Of String)
,该方法将返回 String.Empty。 如果 values
null
,该方法将引发 ArgumentNullException 异常。
Concat(IEnumerable<String>) 是一种方便的方法,可用于连接 IEnumerable(Of String)
集合中的每个元素,而无需先将元素转换为字符串数组。 它特别适用于 Language-Integrated 查询(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
// This example uses the F# Seq.filter function instead of Linq.
open System
let getAlphabet upper =
let charValue = if upper then 65 else 97
seq {
for i = 0 to 25 do
charValue + i |> char |> string
}
getAlphabet true
|> Seq.filter (fun letter -> letter.CompareTo "M" >= 0)
|> String.Concat
|> printfn "%s"
// 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(ReadOnlySpan<Char>, ReadOnlySpan<Char>)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接两个指定的只读字符范围的字符串表示形式。
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<T>(IEnumerable<T>)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
连接 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 扩展方法以提取其 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
// This example uses the F# Seq.filter function instead of Linq.
open System
type Animal =
{ Kind: string
Order: string }
override this.ToString() =
this.Kind
let animals = ResizeArray()
animals.Add { Kind = "Squirrel"; Order = "Rodent" }
animals.Add { Kind = "Gray Wolf"; Order = "Carnivora" }
animals.Add { Kind = "Capybara"; Order = "Rodent" }
Seq.filter (fun animal -> animal.Order = "Rodent")
|> String.Concat
|> printfn "%s"
// 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 查询表达式(LINQ)查询表达式。
IEnumerable<T> 集合中每个对象的字符串表示形式是通过调用该对象的 ToString
方法派生的。