String.Concat Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Przeciążenia
Concat(IEnumerable<String>) |
Łączy elementy członkowskie skonstruowanej IEnumerable<T> kolekcji typu String . |
Concat(String, String, String, String) |
Łączy cztery określone wystąpienia klasy String . |
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
Łączy reprezentacje ciągu czterech określonych zakresów znaków tylko do odczytu. |
Concat(String, String, String) |
Łączy trzy określone wystąpienia klasy String . |
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
Łączy reprezentacje ciągu trzech określonych zakresów znaków tylko do odczytu. |
Concat(Object, Object, Object) |
Łączy reprezentacje ciągów trzech określonych obiektów. |
Concat(Object, Object, Object, Object) |
Łączy reprezentacje ciągów czterech określonych obiektów i obiektów określonych na opcjonalnej liście parametrów o zmiennej długości. |
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
Łączy reprezentacje ciągu dwóch określonych zakresów znaków tylko do odczytu. |
Concat(Object, Object) |
Łączy reprezentacje ciągu dwóch określonych obiektów. |
Concat(String[]) |
Łączy elementy określonej String tablicy. |
Concat(Object[]) |
Łączy reprezentacje ciągów elementów w określonej Object tablicy. |
Concat(Object) |
Tworzy reprezentację ciągu określonego obiektu. |
Concat(String, String) |
Łączy dwa określone wystąpienia klasy String . |
Concat<T>(IEnumerable<T>) |
Łączy elementy członkowskie IEnumerable<T> implementacji. |
Uwagi
Uwaga
Do połączenia ciągów można również użyć operatora konkasowania ciągów języka, takiego jak w języku C# lub Visual Basic w +
&
+
języku . Oba kompilatory tłumaczą operator połączenia na wywołanie jednego z przeciążeń metody String.Concat
.
Concat(IEnumerable<String>)
Łączy elementy członkowskie skonstruowanej IEnumerable<T> kolekcji typu 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
Parametry
- values
- IEnumerable<String>
Obiekt kolekcji, który implementuje argument IEnumerable<T> typu ogólnego i ma wartość String .
Zwraca
Ciągi zeskontenowane w values
, lub , jeśli element jest Empty values
IEnumerable(Of String)
pusty.
- Atrybuty
Wyjątki
values
to null
.
Przykłady
W poniższym przykładzie użyto algorytmu nazwanego sitem Eratostenesa do obliczania liczb pierwszych, których wartość jest równa 100 lub mniejsza. Przypisuje wynik do obiektu typu List<T> , który następnie przekazuje do metody 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
Uwagi
Metoda łączy każdy obiekt w values
pliku ; nie dodaje żadnych ograniczników. Aby określić ogranicznik między poszczególnymi członkami values
, wywołaj Join(String, IEnumerable<String>) metodę .
Ciąg Empty jest używany w miejsce dowolnego elementu o wartości null w values
elemencie .
Jeśli values
wartość jest IEnumerable(Of String)
pusta, metoda zwraca wartość String.Empty . Jeśli values
jest , metoda zgłasza null
ArgumentNullException wyjątek.
Concat(IEnumerable<String>) jest wygodną metodą, która pozwala na połączenia każdego elementu w kolekcji bez wcześniejszego IEnumerable(Of String)
konwertowania elementów na tablicę ciągów. Jest to szczególnie przydatne w Language-Integrated zapytań ZAPYTAŃ (LINQ). Poniższy przykład przekazuje obiekt zawierający wielkie lub małe litery alfabetu do wyrażenia lambda, które wybiera litery, które są równe lub większe niż określonej litery (w tym przykładzie jest List(Of String)
to "M"). Kolekcja zwracana przez metodę jest przekazywana do metody w celu wyświetlenia IEnumerable(Of String)
wyniku w postaci pojedynczego Enumerable.Where Concat(IEnumerable<String>) ciągu.
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
Dotyczy
Concat(String, String, String, String)
Łączy cztery określone wystąpienia klasy 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
Parametry
- str0
- String
Pierwszy ciąg do połączenia.
- str1
- String
Drugi ciąg do połączenia.
- str2
- String
Trzeci ciąg do połączenia.
- str3
- String
Czwarty ciąg do połączenia.
Zwraca
Concatenation of str0
, , , and str1
str2
str3
.
Przykłady
Poniższy przykład definiuje tablicę czteroliterowych wyrazów i przechowuje ich poszczególne litery w tablicy ciągów w celu ich rozsyłania. Następnie wywołuje metodę Concat(String, String, String, String) w celu ponownego zsadowania sypkich wyrazów.
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
Uwagi
Metoda łączy , str0
str1
, str2
i str3
; nie dodaje żadnych ograniczników.
Zobacz też
Dotyczy
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)
Łączy reprezentacje ciągu czterech określonych zakresów znaków tylko do odczytu.
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
Parametry
- str0
- ReadOnlySpan<Char>
Pierwszy zakres znaków tylko do odczytu do zsyłania.
- str1
- ReadOnlySpan<Char>
Drugi zakres znaków tylko do odczytu do zsyłania.
- str2
- ReadOnlySpan<Char>
Trzeci zakres znaków tylko do odczytu do zsyłania.
- str3
- ReadOnlySpan<Char>
Czwarty zakres znaków tylko do odczytu do zsyłania.
Zwraca
Reprezentacje łączynych ciągów wartości str0
, str1
i str2
str3
.
Dotyczy
Concat(String, String, String)
Łączy trzy określone wystąpienia klasy 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
Parametry
- str0
- String
Pierwszy ciąg do połączenia.
- str1
- String
Drugi ciąg do połączenia.
- str2
- String
Trzeci ciąg do połączenia.
Zwraca
Czenie , str0
str1
i str2
.
Przykłady
W poniższym przykładzie użyto Concat metody w celu połączenia trzech ciągów i wyświetlenie wyniku.
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.
Uwagi
Metoda łączy , str0
str1
i ; nie dodaje str2
żadnych ograniczników.
Zobacz też
Dotyczy
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)
Łączy reprezentacje ciągu trzech określonych zakresów znaków tylko do odczytu.
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
Parametry
- str0
- ReadOnlySpan<Char>
Pierwszy zakres znaków tylko do odczytu do zsyłania.
- str1
- ReadOnlySpan<Char>
Drugi zakres znaków tylko do odczytu do zsyłania.
- str2
- ReadOnlySpan<Char>
Trzeci znak tylko do odczytu obejmuje do zsyłania.
Zwraca
Reprezentacje ciągów zeskontenowanych wartości str0
, str1
i str2
.
Dotyczy
Concat(Object, Object, Object)
Łączy reprezentacje ciągów trzech określonych obiektów.
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
Parametry
- arg0
- Object
Pierwszy obiekt do zsyłania.
- arg1
- Object
Drugi obiekt do zsyłania.
- arg2
- Object
Trzeci obiekt do zsyłania.
Zwraca
Reprezentacje ciągów zeskontenowanych wartości arg0
, arg1
i arg2
.
Przykłady
W poniższym przykładzie pokazano Concat metodę .
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
Uwagi
Metoda łączy , i przez wywołanie metody bez parametrów każdego obiektu; nie arg0
arg1
dodaje żadnych arg2
ToString
ograniczników.
String.Empty Jest używany w miejsce dowolnego argumentu o wartości null.
Zobacz też
Dotyczy
Concat(Object, Object, Object, Object)
Ważne
Ten interfejs API nie jest zgodny ze specyfikacją CLS.
Łączy reprezentacje ciągów czterech określonych obiektów i obiektów określonych na opcjonalnej liście parametrów o zmiennej długości.
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
Parametry
- arg0
- Object
Pierwszy obiekt do zsyłania.
- arg1
- Object
Drugi obiekt do zsyłania.
- arg2
- Object
Trzeci obiekt do zsyłania.
- arg3
- Object
Czwarty obiekt do zsyłania.
Zwraca
Reprezentacja wszystkich wartości na liście parametrów w postaci sparatenowanych ciągów.
- Atrybuty
Przykłady
Poniższy przykład ilustruje użycie metody do zsyłania Concat(Object, Object, Object, Object) listy parametrów zmiennych. W tym przypadku metoda jest wywoływana z dziewięcioma parametrami.
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
Uwagi
Uwaga
Ten interfejs API nie jest zgodny ze specyfikacją CLS. Alternatywą zgodną ze specyfikacją CLS jest String.Concat(Object[]) . Kompilatory języka C# Visual Basic automatycznie rozpoznają wywołanie tej metody jako wywołanie metody String.Concat(Object[]) .
Metoda łączy każdy obiekt na liście parametrów, wywołując jego metodę bez parametrów; nie ToString
dodaje żadnych ograniczników.
String.Empty Jest używany w miejsce dowolnego argumentu o wartości null.
Uwaga
Ostatnim parametrem metody jest opcjonalna lista rozdzielonych przecinkami jednego lub Concat większej liczby dodatkowych obiektów do zsyłania.
Uwagi dotyczące wywoływania
Ta metoda jest oznaczona słowem kluczowym , co oznacza, że obsługuje vararg
zmienną liczbę parametrów. Metoda może być wywoływana z Visual C++, ale nie może być wywoływana z języka C# ani Visual Basic kodu. Kompilatory języka C# i Visual Basic rozpoznają wywołania do Concat(Object, Object, Object, Object) jako wywołania . Concat(Object[])
Dotyczy
Concat(ReadOnlySpan<Char>, ReadOnlySpan<Char>)
Łączy reprezentacje ciągu dwóch określonych zakresów znaków tylko do odczytu.
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
Parametry
- str0
- ReadOnlySpan<Char>
Pierwszy zakres znaków tylko do odczytu do zsyłania.
- str1
- ReadOnlySpan<Char>
Drugi zakres znaków tylko do odczytu do zsyłania.
Zwraca
Reprezentacje ciągów zeskontenowanych wartości str0
i str1
.
Dotyczy
Concat(Object, Object)
Łączy reprezentacje ciągu dwóch określonych obiektów.
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
Parametry
- arg0
- Object
Pierwszy obiekt do zsyłania.
- arg1
- Object
Drugi obiekt do zsyłania.
Zwraca
Reprezentacje ciągów zeskontenowanych wartości arg0
i arg1
.
Przykłady
W poniższym przykładzie pokazano Concat metodę .
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
Uwagi
Metoda łączy metody i przez wywołanie metody bez parametrów i ; nie arg0
arg1
dodaje żadnych ToString
arg0
arg1
ograniczników.
String.Empty Jest używany w miejsce dowolnego argumentu o wartości null.
Jeśli którykolwiek z argumentów jest odwołaniem do tablicy, metoda łączy ciąg reprezentujący tę tablicę, a nie jej składowe (na przykład "System.String[]").
Zobacz też
Dotyczy
Concat(String[])
Ważne
Ten interfejs API nie jest zgodny ze specyfikacją CLS.
Łączy elementy określonej String tablicy.
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
Parametry
- values
- String[]
Tablica wystąpień ciągów.
Zwraca
Elementy concatenated values
.
- Atrybuty
Wyjątki
values
to null
.
Za mało pamięci.
Przykłady
W poniższym przykładzie pokazano użycie Concat metody z String tablicą .
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
Uwagi
Metoda łączy każdy obiekt w values
pliku ; nie dodaje żadnych ograniczników.
Ciąg Empty jest używany w miejsce dowolnego obiektu o wartości null w tablicy.
Zobacz też
Dotyczy
Concat(Object[])
Łączy reprezentacje ciągów elementów w określonej Object tablicy.
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
Parametry
- args
- Object[]
Tablica obiektów zawierająca elementy do zsyłania.
Zwraca
Reprezentacje ciągów zeskontenowanych wartości elementów w args
.
Wyjątki
args
to null
.
Za mało pamięci.
Przykłady
W poniższym przykładzie pokazano użycie metody Concat z Object tablicą .
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
Uwagi
Metoda łączy każdy obiekt w metodzie przez wywołanie metody bez parametrów tego obiektu; nie args
ToString
dodaje żadnych ograniczników.
String.Empty Jest używany w miejsce dowolnego obiektu o wartości null w tablicy .
Uwagi dotyczące wywoływania
Ta metoda nie jest wywoływana przez kod C++. Kompilator języka C++ rozwiązuje wywołania do , które mają co najmniej cztery parametry Concat obiektu jako wywołanie Concat(Object, Object, Object, Object) .
Zobacz też
Dotyczy
Concat(Object)
Tworzy reprezentację ciągu określonego obiektu.
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
Parametry
- arg0
- Object
Obiekt, który ma być reprezentowany, lub null
.
Zwraca
Reprezentacja ciągu wartości arg0
, lub , Empty jeśli jest arg0
null
.
Przykłady
W poniższym przykładzie pokazano Concat metodę .
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
Uwagi
Metoda Concat(Object) reprezentuje jako arg0
ciąg, wywołując jej metodę bez ToString
parametrów.
Zobacz też
Dotyczy
Concat(String, String)
Łączy dwa określone wystąpienia klasy 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
Parametry
- str0
- String
Pierwszy ciąg do połączenia.
- str1
- String
Drugi ciąg do połączenia.
Zwraca
Czenie i str0
str1
.
Przykłady
Poniższy przykład łączy imię, środkowe i nazwisko osoby.
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'!
Uwagi
Metoda łączy i str0
str1
; nie dodaje żadnych ograniczników.
Ciąg Empty jest używany w miejsce dowolnego argumentu o wartości null.
Zobacz też
Dotyczy
Concat<T>(IEnumerable<T>)
Łączy elementy członkowskie IEnumerable<T> implementacji.
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
Parametry typu
- T
Typ elementów członkowskich values
.
Parametry
- values
- IEnumerable<T>
Obiekt kolekcji, który implementuje IEnumerable<T> interfejs.
Zwraca
Elementy członkowskie w programie values
.
- Atrybuty
Wyjątki
values
to null
.
Przykłady
W poniższym przykładzie zdefiniowano bardzo prostą klasę, która zawiera nazwę zwierząt i Animal
kolejność, do której należy. Następnie definiuje obiekt List<T> do zawiera wiele Animal
obiektów. Metoda Enumerable.Where rozszerzenia jest wywoływana w celu wyodrębnienia Animal
obiektów, których właściwość jest równa Order
"Rodent". Wynik jest przekazywany do Concat<T>(IEnumerable<T>) metody i wyświetlany w konsoli.
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
Uwagi
Metoda łączy każdy obiekt w pliku values
; nie dodaje żadnych ograniczników.
Ciąg Empty jest używany w miejsce dowolnego argumentu o wartości null.
Concat<T>(IEnumerable<T>) jest wygodną metodą, która umożliwia ujednanie każdego elementu w kolekcji bez wcześniejszego IEnumerable<T> konwertowania elementów na ciągi. Jest to szczególnie przydatne w Language-Integrated zapytań (LINQ), jak pokazano w przykładzie. Reprezentacja ciągu każdego obiektu w IEnumerable<T> kolekcji jest uzyskiwana przez wywołanie metody tego ToString
obiektu.