String.Split Methode
Definition
Gibt ein Zeichenfolgenarray zurück, das die Teilzeichenfolgen dieser Instanz enthält, die durch Elemente eines angegebenen Zeichenfolgen- oder Unicode-Zeichenarrays getrennt sind.Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.
Überlädt
Split(Char[], Int32, StringSplitOptions) |
Hier wird eine Zeichenfolge anhand von angegebenen Trennzeichen und optional von Optionen in die maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on specified delimiting characters and, optionally, options. |
Split(Char, Int32, StringSplitOptions) |
Hier wird eine Zeichenfolge anhand eines angegebenen Trennzeichens und optional von Optionen in die maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on a specified delimiting character and, optionally, options. Hier wird eine Zeichenfolge in eine maximale Anzahl von Teilzeichenfolgen anhand des angegebenen Ersatztrennzeichens unterteilt, wobei optional leere Teilzeichenfolgen aus dem Ergebnis ausgelassen werden.Splits a string into a maximum number of substrings based on the provided character separator, optionally omitting empty substrings from the result. |
Split(String[], Int32, StringSplitOptions) |
Hier wird eine Zeichenfolge anhand von angegebenen Trennzeichenfolgen und optional von Optionen in die maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on specified delimiting strings and, optionally, options. |
Split(String[], StringSplitOptions) |
Hier wird eine Zeichenfolge anhand einer angegebenen Trennzeichenfolge und optional von Optionen in Teilzeichenfolgen unterteilt.Splits a string into substrings based on a specified delimiting string and, optionally, options. |
Split(String, Int32, StringSplitOptions) |
Hier wird eine Zeichenfolge anhand einer angegebenen Trennzeichenfolge und optional von Optionen in die maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on a specified delimiting string and, optionally, options. |
Split(Char[], StringSplitOptions) |
Hier wird eine Zeichenfolge anhand der angegebenen Trennzeichen und der Optionen in Teilzeichenfolgen unterteilt.Splits a string into substrings based on specified delimiting characters and options. |
Split(Char[], Int32) |
Hier wird eine Zeichenfolge anhand der angegebenen Trennzeichen in eine maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on specified delimiting characters. |
Split(Char, StringSplitOptions) |
Hier wird eine Zeichenfolge anhand eines angegebenen Trennzeichens und optional von Optionen in Teilzeichenfolgen unterteilt.Splits a string into substrings based on a specified delimiting character and, optionally, options. |
Split(String, StringSplitOptions) |
Unterteilt eine Zeichenfolge anhand der angegebenen Zeichenfolgentrennlinie in Teilzeichenfolgen.Splits a string into substrings that are based on the provided string separator. |
Split(Char[]) |
Hier wird eine Zeichenfolge anhand der angegebenen Trennzeichen in Teilzeichenfolgen unterteilt.Splits a string into substrings based on specified delimiting characters. |
Hinweise
Split wird verwendet, um eine Zeichenfolge mit Trennzeichen in Teil Zeichenfolgen zu zerlegen.Split is used to break a delimited string into substrings. Sie können entweder ein Zeichen Array oder ein Zeichen folgen Array verwenden, um NULL oder mehr Begrenzungs Zeichen oder Zeichen folgen anzugeben.You can use either a character array or a string array to specify zero or more delimiting characters or strings. Wenn keine Trennzeichen angegeben werden, wird die Zeichenfolge bei Leerzeichen aufgeteilt.If no delimiting characters are specified, the string is split at white-space characters.
Über Ladungen der Split -Methode ermöglichen es Ihnen, die Anzahl der von der-Methode (der-Methode) zurückgegebenen Teil Zeichenfolgen einzuschränken Split(Char[], Int32) , um anzugeben, ob leere Zeichen folgen und/oder Trim-Teil Zeichenfolgen im Ergebnis (die-Methode und die-Methode) eingeschlossen Split(Char[], StringSplitOptions) Split(String[], StringSplitOptions) werden sollen Split(Char[], Int32, StringSplitOptions) Split(String[], Int32, StringSplitOptions) .Overloads of the Split method allow you to limit the number of substrings returned by the method (the Split(Char[], Int32) method), to specify whether to include empty strings and/or trim substrings in the result (the Split(Char[], StringSplitOptions) and Split(String[], StringSplitOptions) methods), or to do both (the Split(Char[], Int32, StringSplitOptions) and Split(String[], Int32, StringSplitOptions) methods).
Tipp
Die- Split Methode ist nicht immer die beste Möglichkeit, eine Zeichenfolge mit Trennzeichen in Teil Zeichenfolgen zu unterteilen.The Split method is not always the best way to break a delimited string into substrings. Wenn Sie nicht alle Teil Zeichenfolgen einer Zeichenfolge mit Trennzeichen extrahieren möchten oder wenn Sie eine Zeichenfolge auf der Grundlage eines Musters anstelle eines Satzes von Trennzeichen analysieren möchten, sollten Sie die Verwendung regulärer Ausdrücke in Erwägung ziehen oder eine der Suchmethoden kombinieren, die den Index eines Zeichens mit der-Methode zurückgibt Substring .If you don't want to extract all of the substrings of a delimited string, or if you want to parse a string based on a pattern instead of a set of delimiter characters, consider using regular expressions, or combine one of the search methods that returns the index of a character with the Substring method. Weitere Informationen finden Sie unter Extrahieren von Teil Zeichenfolgen aus einer Zeichenfolge.For more information, see Extract substrings from a string.
BeispielExample
Die folgenden Beispiele zeigen drei verschiedene Überladungen von String.Split()
.The following examples show three different overloads of String.Split()
. Im ersten Beispiel wird die-Überladung aufgerufen, Split(Char[]) und ein einzelnes Trennzeichen wird übergeben.The first example calls the Split(Char[]) overload and passes in a single delimiter.
string s = "You win some. You lose some.";
string[] subs = s.Split(' ');
foreach (var sub in subs)
{
Console.WriteLine($"Substring: {sub}");
}
// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some.
// Substring: You
// Substring: lose
// Substring: some.
Dim s As String = "You win some. You lose some."
Dim subs As String() = s.Split()
For Each substring As String In subs
Console.WriteLine($"Substring: {substring}")
Next
' This example produces the following output:
'
' Substring: You
' Substring: win
' Substring: some.
' Substring: You
' Substring: lose
' Substring: some.
Wie Sie sehen können, sind die Punktzeichen (.
) in zwei der Teilzeichenfolgen enthalten.As you can see, the period characters (.
) are included in two of the substrings. Wenn Sie die Punktzeichen ausschließen möchten, können Sie das Punktzeichen als zusätzliches Trennzeichen hinzufügen.If you want to exclude the period characters, you can add the period character as an additional delimiting character. Im nächsten Beispiel wird gezeigt, wie dies geschieht.The next example shows how to do this.
string s = "You win some. You lose some.";
string[] subs = s.Split(' ', '.');
foreach (var sub in subs)
{
Console.WriteLine($"Substring: {sub}");
}
// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some
// Substring:
// Substring: You
// Substring: lose
// Substring: some
// Substring:
Dim s As String = "You win some. You lose some."
Dim subs As String() = s.Split(" "c, "."c)
For Each substring As String In subs
Console.WriteLine($"Substring: {substring}")
Next
' This example produces the following output:
'
' Substring: You
' Substring: win
' Substring: some
' Substring:
' Substring: You
' Substring: lose
' Substring: some
' Substring:
Die Punkte sind aus den Teilzeichenfolgen verschwunden, aber jetzt wurden zwei zusätzliche leere Teilzeichenfolgen eingefügt.The periods are gone from the substrings, but now two extra empty substrings have been included. Diese leeren Teil Zeichenfolge stellt die Teil Zeichenfolge zwischen einem Wort und dem nachfolg folgenden Zeitraum dar.These empty substring represent the substring between a word and the period that follows it. Um leere Teilzeichenfolgen aus dem resultierenden Array auszuschließen, können Sie die Überladung Split(Char[], StringSplitOptions) aufrufen und StringSplitOptions.RemoveEmptyEntries für den Parameter options
angeben.To omit empty substrings from the resulting array, you can call the Split(Char[], StringSplitOptions) overload and specify StringSplitOptions.RemoveEmptyEntries for the options
parameter.
string s = "You win some. You lose some.";
char[] separators = new char[] { ' ', '.' };
string[] subs = s.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (var sub in subs)
{
Console.WriteLine($"Substring: {sub}");
}
// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some
// Substring: You
// Substring: lose
// Substring: some
Dim s As String = "You win some. You lose some."
Dim separators As Char() = New Char() {" "c, "."c}
Dim subs As String() = s.Split(separators, StringSplitOptions.RemoveEmptyEntries)
For Each substring As String In subs
Console.WriteLine($"Substring: {substring}")
Next
' This example produces the following output:
'
' Substring: You
' Substring: win
' Substring: some
' Substring: You
' Substring: lose
' Substring: some
Die Abschnitte für die einzelnen über Ladungen von String.Split()
enthalten weitere Beispiele.The sections for the individual overloads of String.Split()
contain further examples.
Split(Char[], Int32, StringSplitOptions)
Hier wird eine Zeichenfolge anhand von angegebenen Trennzeichen und optional von Optionen in die maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on specified delimiting characters and, optionally, options.
public:
cli::array <System::String ^> ^ Split(cli::array <char> ^ separator, int count, StringSplitOptions options);
public string[] Split (char[] separator, int count, StringSplitOptions options);
public string[] Split (char[]? separator, int count, StringSplitOptions options);
[System.Runtime.InteropServices.ComVisible(false)]
public string[] Split (char[] separator, int count, StringSplitOptions options);
member this.Split : char[] * int * StringSplitOptions -> string[]
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Split : char[] * int * StringSplitOptions -> string[]
Public Function Split (separator As Char(), count As Integer, options As StringSplitOptions) As String()
Parameter
- separator
- Char[]
Hierbei handelt es sich um ein Array von Zeichen, die die Teilzeichenfolgen dieser Zeichenfolge trennen, ein leeres Array ohne Trennzeichen oder null
.An array of characters that delimit the substrings in this string, an empty array that contains no delimiters, or null
.
- count
- Int32
Die maximale Anzahl der zurückzugebenden Teilzeichenfolgen.The maximum number of substrings to return.
- options
- StringSplitOptions
Dies ist eine bitweise Kombination der Enumerationswerte, die angibt, ob Teilzeichenfolgen zugeschnitten und leere Teilzeichenfolgen eingeschlossen werden sollen.A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.
Gibt zurück
- String[]
Ein Array, das die Teilzeichenfolgen in dieser Zeichenfolge enthält, die durch ein oder mehr Zeichen aus separator
getrennt sind.An array that contains the substrings in this string that are delimited by one or more characters in separator
. Weitere Informationen finden Sie im Abschnitt "Hinweise".For more information, see the Remarks section.
- Attribute
Ausnahmen
count
ist ein negativer Wert.count
is negative.
options
entspricht keinem der StringSplitOptions-Werte.options
is not one of the StringSplitOptions values.
Beispiele
Im folgenden Beispiel wird die- StringSplitOptions Enumeration verwendet, um von der-Methode generierte Teil Zeichenfolgen einzuschließen oder auszuschließen Split .The following example uses the StringSplitOptions enumeration to include or exclude substrings generated by the Split method.
// This example demonstrates the String.Split(Char[], Boolean) and
// String.Split(Char[], Int32, Boolean) methods
using namespace System;
void Show( array<String^>^entries )
{
Console::WriteLine( "The return value contains these {0} elements:", entries->Length );
System::Collections::IEnumerator^ myEnum = entries->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ entry = safe_cast<String^>(myEnum->Current);
Console::Write( "<{0}>", entry );
}
Console::Write( "{0}{0}", Environment::NewLine );
}
int main()
{
String^ s = ",one,,,two,,,,,three,,";
array<Char>^sep = gcnew array<Char>{
','
};
array<String^>^result;
//
Console::WriteLine( "The original string is \"{0}\".", s );
Console::WriteLine( "The separation character is '{0}'.", sep[ 0 ] );
Console::WriteLine();
//
Console::WriteLine( "Split the string and return all elements:" );
result = s->Split( sep, StringSplitOptions::None );
Show( result );
//
Console::WriteLine( "Split the string and return all non-empty elements:" );
result = s->Split( sep, StringSplitOptions::RemoveEmptyEntries );
Show( result );
//
Console::WriteLine( "Split the string and return 2 elements:" );
result = s->Split( sep, 2, StringSplitOptions::None );
Show( result );
//
Console::WriteLine( "Split the string and return 2 non-empty elements:" );
result = s->Split( sep, 2, StringSplitOptions::RemoveEmptyEntries );
Show( result );
}
/*
This example produces the following results:
The original string is ",one,,,two,,,,,three,,".
The separation character is ','.
Split the string and return all elements:
The return value contains these 12 elements:
<><one><><><two><><><><><three><><>
Split the string and return all non-empty elements:
The return value contains these 3 elements:
<one><two><three>
Split the string and return 2 elements:
The return value contains these 2 elements:
<><one,,,two,,,,,three,,>
Split the string and return 2 non-empty elements:
The return value contains these 2 elements:
<one><,,two,,,,,three,,>
*/
// This example demonstrates the String.Split() methods that use
// the StringSplitOptions enumeration.
string s1 = ",ONE,,TWO,,,THREE,,";
string s2 = "[stop]" +
"ONE[stop][stop]" +
"TWO[stop][stop][stop]" +
"THREE[stop][stop]";
char[] charSeparators = new char[] { ',' };
string[] stringSeparators = new string[] { "[stop]" };
string[] result;
// ------------------------------------------------------------------------------
// Split a string delimited by characters.
// ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:\n");
// Display the original string and delimiter characters.
Console.WriteLine($"1a) The original string is \"{s1}\".");
Console.WriteLine($"The delimiter character is '{charSeparators[0]}'.\n");
// Split a string delimited by characters and return all elements.
Console.WriteLine("1b) Split a string delimited by characters and " +
"return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);
// Split a string delimited by characters and return all non-empty elements.
Console.WriteLine("1c) Split a string delimited by characters and " +
"return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Split the original string into the string and empty string before the
// delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("1d) Split a string delimited by characters and " +
"return 2 elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);
// Split the original string into the string after the delimiter and the
// remainder of the original string after the delimiter.
Console.WriteLine("1e) Split a string delimited by characters and " +
"return 2 non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// ------------------------------------------------------------------------------
// Split a string delimited by another string.
// ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:\n");
// Display the original string and delimiter string.
Console.WriteLine($"2a) The original string is \"{s2}\".");
Console.WriteLine($"The delimiter string is \"{stringSeparators[0]}\".\n");
// Split a string delimited by another string and return all elements.
Console.WriteLine("2b) Split a string delimited by another string and " +
"return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);
// Split the original string at the delimiter and return all non-empty elements.
Console.WriteLine("2c) Split a string delimited by another string and " +
"return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Split the original string into the empty string before the
// delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("2d) Split a string delimited by another string and " +
"return 2 elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);
// Split the original string into the string after the delimiter and the
// remainder of the original string after the delimiter.
Console.WriteLine("2e) Split a string delimited by another string and " +
"return 2 non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Display the array of separated strings using a local function
void Show(string[] entries)
{
Console.WriteLine($"The return value contains these {entries.Length} elements:");
foreach (string entry in entries)
{
Console.Write($"<{entry}>");
}
Console.Write("\n\n");
}
/*
This example produces the following results:
1) Split a string delimited by characters:
1a) The original string is ",ONE,,TWO,,,THREE,,".
The delimiter character is ','.
1b) Split a string delimited by characters and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>
1c) Split a string delimited by characters and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>
1d) Split a string delimited by characters and return 2 elements:
The return value contains these 2 elements:
<><ONE,,TWO,,,THREE,,>
1e) Split a string delimited by characters and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO,,,THREE,,>
2) Split a string delimited by another string:
2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
The delimiter string is "[stop]".
2b) Split a string delimited by another string and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>
2c) Split a string delimited by another string and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>
2d) Split a string delimited by another string and return 2 elements:
The return value contains these 2 elements:
<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
2e) Split a string delimited by another string and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
*/
Dim s1 As String = ",ONE,,TWO,,,THREE,,"
Dim s2 As String = "[stop]" &
"ONE[stop][stop]" &
"TWO[stop][stop][stop]" &
"THREE[stop][stop]"
Dim charSeparators() As Char = {","c}
Dim stringSeparators() As String = {"[stop]"}
Dim result() As String
' ------------------------------------------------------------------------------
' Split a string delimited by characters.
' ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:" & vbCrLf)
' Display the original string and delimiter characters.
Console.WriteLine("1a) The original string is ""{0}"".", s1)
Console.WriteLine("The delimiter character is '{0}'." & vbCrLf, charSeparators(0))
' Split a string delimited by characters and return all elements.
Console.WriteLine("1b) Split a string delimited by characters and " &
"return all elements:")
result = s1.Split(charSeparators, StringSplitOptions.None)
Show(result)
' Split a string delimited by characters and return all non-empty elements.
Console.WriteLine("1c) Split a string delimited by characters and " &
"return all non-empty elements:")
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' Split the original string into the string and empty string before the
' delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("1d) Split a string delimited by characters and " &
"return 2 elements:")
result = s1.Split(charSeparators, 2, StringSplitOptions.None)
Show(result)
' Split the original string into the string after the delimiter and the
' remainder of the original string after the delimiter.
Console.WriteLine("1e) Split a string delimited by characters and " &
"return 2 non-empty elements:")
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' ------------------------------------------------------------------------------
' Split a string delimited by another string.
' ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:" & vbCrLf)
' Display the original string and delimiter string.
Console.WriteLine("2a) The original string is ""{0}"".", s2)
Console.WriteLine("The delimiter string is ""{0}""." & vbCrLf, stringSeparators(0))
' Split a string delimited by another string and return all elements.
Console.WriteLine("2b) Split a string delimited by another string and " &
"return all elements:")
result = s2.Split(stringSeparators, StringSplitOptions.None)
Show(result)
' Split the original string at the delimiter and return all non-empty elements.
Console.WriteLine("2c) Split a string delimited by another string and " &
"return all non-empty elements:")
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' Split the original string into the empty string before the
' delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("2d) Split a string delimited by another string and " &
"return 2 elements:")
result = s2.Split(stringSeparators, 2, StringSplitOptions.None)
Show(result)
' Split the original string into the string after the delimiter and the
' remainder of the original string after the delimiter.
Console.WriteLine("2e) Split a string delimited by another string and " &
"return 2 non-empty elements:")
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Show(result)
End Sub
' Display the array of separated strings.
Public Shared Sub Show(ByVal entries() As String)
Console.WriteLine("The return value contains these {0} elements:", entries.Length)
Dim entry As String
For Each entry In entries
Console.Write("<{0}>", entry)
Next entry
Console.Write(vbCrLf & vbCrLf)
End Sub
'This example produces the following results:
'
'1) Split a string delimited by characters:
'
'1a) The original string is ",ONE,,TWO,,,THREE,,".
'The delimiter character is ','.
'
'1b) Split a string delimited by characters and return all elements:
'The return value contains these 9 elements:
'<><ONE><><TWO><><><THREE><><>
'
'1c) Split a string delimited by characters and return all non-empty elements:
'The return value contains these 3 elements:
'<ONE><TWO><THREE>
'
'1d) Split a string delimited by characters and return 2 elements:
'The return value contains these 2 elements:
'<><ONE,,TWO,,,THREE,,>
'
'1e) Split a string delimited by characters and return 2 non-empty elements:
'The return value contains these 2 elements:
'<ONE><TWO,,,THREE,,>
'
'2) Split a string delimited by another string:
'
'2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
'The delimiter string is "[stop]".
'
'2b) Split a string delimited by another string and return all elements:
'The return value contains these 9 elements:
'<><ONE><><TWO><><><THREE><><>
'
'2c) Split a string delimited by another string and return all non-empty elements:
'The return value contains these 3 elements:
'<ONE><TWO><THREE>
'
'2d) Split a string delimited by another string and return 2 elements:
'The return value contains these 2 elements:
'<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
'
'2e) Split a string delimited by another string and return 2 non-empty elements:
'The return value contains these 2 elements:
'<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
'
Hinweise
Trennzeichen sind nicht in den Elementen des zurückgegebenen Arrays enthalten.Delimiter characters are not included in the elements of the returned array.
Wenn diese Instanz keines der Zeichen in enthält separator
oder der- count
Parameter 1 ist, besteht das zurückgegebene Array aus einem einzelnen-Element, das diese Instanz enthält.If this instance does not contain any of the characters in separator
, or the count
parameter is 1, the returned array consists of a single element that contains this instance.
Wenn der- separator
Parameter ist null
oder keine Zeichen enthält, wird davon ausgegangen, dass Leerzeichen als Trennzeichen verwendet werden.If the separator
parameter is null
or contains no characters, white-space characters are assumed to be the delimiters. Leerzeichen werden durch den Unicode-Standard definiert, und die Char.IsWhiteSpace Methode gibt zurück true
, wenn Sie an Sie übermittelt werden.White-space characters are defined by the Unicode standard and the Char.IsWhiteSpace method returns true
if they are passed to it.
Um null
für den- char[] separator
Parameter zu übergeben, müssen Sie den Typ der angeben, null
um den-Befehl von einigen anderen über Ladungen, z. b., zu unterscheiden Split(String[], Int32, StringSplitOptions) .To pass null
for the char[] separator
parameter, you must indicate the type of the null
to disambiguate the call from some other overloads, such as Split(String[], Int32, StringSplitOptions). Im folgenden Beispiel werden mehrere Möglichkeiten zum eindeutigen Identifizieren dieser Überladung veranschaulicht.The following example shows several ways to unambiguously identify this overload.
string phrase = "The quick brown fox";
_ = phrase.Split(default(char[]), 3, StringSplitOptions.RemoveEmptyEntries);
_ = phrase.Split((char[])null, 3, StringSplitOptions.RemoveEmptyEntries);
_ = phrase.Split(null as char[], 3, StringSplitOptions.RemoveEmptyEntries);
Dim phrase As String = "The quick brown fox"
Dim words() As String
words = phrase.Split(TryCast(Nothing, Char()), 3,
StringSplitOptions.RemoveEmptyEntries)
words = phrase.Split(New Char() {}, 3,
StringSplitOptions.RemoveEmptyEntries)
Wenn der- count
Parameter 0 (null) ist, oder wenn der options
-Parameter ist RemoveEmptyEntries und die Länge dieser Instanz 0 (null) ist, wird ein leeres Array zurückgegeben.If the count
parameter is zero, or the options
parameter is RemoveEmptyEntries and the length of this instance is zero, an empty array is returned.
Jedes Element von separator
definiert ein separates Trennzeichen.Each element of separator
defines a separate delimiter character. Wenn der options
-Parameter ist None und zwei Trennzeichen nebeneinander liegen oder ein Trennzeichen am Anfang oder Ende dieser Instanz gefunden wird, enthält das entsprechende Array Element Empty .If the options
parameter is None, and two delimiters are adjacent or a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty.
Wenn in dieser Instanz mehr als Teil Zeichenfolgen vorhanden sind count
, count
werden die ersten minus 1 Teil Zeichenfolgen in den ersten count
minus 1 Elementen des Rückgabewerts zurückgegeben, und die restlichen Zeichen in dieser Instanz werden im letzten Element des Rückgabewerts zurückgegeben.If there are more than count
substrings in this instance, the first count
minus 1 substrings are returned in the first count
minus 1 elements of the return value, and the remaining characters in this instance are returned in the last element of the return value.
Wenn count
größer als die Anzahl der Teil Zeichenfolgen ist, werden die verfügbaren Teil Zeichenfolgen zurückgegeben, und es wird keine Ausnahme ausgelöst.If count
is greater than the number of substrings, the available substrings are returned and no exception is thrown.
Überlegungen zur LeistungPerformance considerations
Die Split -Methoden weisen Speicher für das zurückgegebene Array Objekt und ein- String Objekt für jedes Array Element zu.The Split methods allocate memory for the returned array object and a String object for each array element. Wenn Ihre Anwendung eine optimale Leistung erfordert oder wenn die Verwaltung der Speicher Belegung in der Anwendung wichtig ist, sollten Sie die IndexOf IndexOfAny -Methode oder die-Methode und optional die- Compare Methode verwenden, um eine Teil Zeichenfolge innerhalb einer Zeichenfolge zu suchen.If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method, and optionally the Compare method, to locate a substring within a string.
Wenn Sie eine Zeichenfolge an einem Trennzeichen aufteilen, verwenden Sie die- IndexOf Methode oder die- IndexOfAny Methode, um ein Trennzeichen in der Zeichenfolge zu suchen.If you are splitting a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. Wenn Sie eine Zeichenfolge in eine Trenn Zeichenfolge aufteilen, verwenden Sie die- IndexOf oder- IndexOfAny Methode, um das erste Zeichen der Trenn Zeichenfolge zu suchen.If you are splitting a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Verwenden Sie dann die- Compare Methode, um zu bestimmen, ob die Zeichen nach dem ersten Zeichen gleich den verbleibenden Zeichen der Trenn Zeichenfolge sind.Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
Wenn außerdem die gleichen Zeichen folgen zum Aufteilen von Zeichen folgen in mehreren Split Methoden aufrufen verwendet werden, sollten Sie in Erwägung gezogen werden, ein einzelnes Array zu erstellen und in jedem Methodenaufruf darauf zu verweisen.In addition, if the same set of characters is used to split strings in multiple Split method calls, consider creating a single array and referencing it in each method call. Dadurch wird der zusätzliche mehr Aufwand für die einzelnen Methodenaufrufe erheblich reduziert.This significantly reduces the additional overhead of each method call.
Hinweise für Aufrufer
In .NET Framework 3.5.NET Framework 3.5 und früheren Versionen Split(Char[]) separator
verwendet die-Methode null
einen etwas anderen Satz von Leerzeichen, um die Zeichenfolge zu teilen, wenn der Methode ein-Wert mit dem Wert oder keine Zeichen enthält Trim(Char[]) .In .NET Framework 3.5.NET Framework 3.5 and earlier versions, if the Split(Char[]) method is passed a separator
that is null
or contains no characters, the method uses a slightly different set of white-space characters to split the string than the Trim(Char[]) method does to trim the string. Beginnend mit .NET Framework 4 verwenden beide Methoden einen identischen Satz von Unicode-Leerzeichen.Starting with .NET Framework 4, both methods use an identical set of Unicode white-space characters.
Gilt für:
Split(Char, Int32, StringSplitOptions)
Hier wird eine Zeichenfolge anhand eines angegebenen Trennzeichens und optional von Optionen in die maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on a specified delimiting character and, optionally, options. Hier wird eine Zeichenfolge in eine maximale Anzahl von Teilzeichenfolgen anhand des angegebenen Ersatztrennzeichens unterteilt, wobei optional leere Teilzeichenfolgen aus dem Ergebnis ausgelassen werden.Splits a string into a maximum number of substrings based on the provided character separator, optionally omitting empty substrings from the result.
public string[] Split (char separator, int count, StringSplitOptions options = System.StringSplitOptions.None);
member this.Split : char * int * StringSplitOptions -> string[]
Public Function Split (separator As Char, count As Integer, Optional options As StringSplitOptions = System.StringSplitOptions.None) As String()
Parameter
- separator
- Char
Ein Zeichen, das die Teilzeichenfolgen in dieser Instanz trennt.A character that delimits the substrings in this instance.
- count
- Int32
Die maximale Anzahl der im Array erwarteten Elemente.The maximum number of elements expected in the array.
- options
- StringSplitOptions
Dies ist eine bitweise Kombination der Enumerationswerte, die angibt, ob Teilzeichenfolgen zugeschnitten und leere Teilzeichenfolgen eingeschlossen werden sollen.A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.
Gibt zurück
- String[]
Ein Array, das maximal count
Teilzeichenfolgen von dieser Instanz enthält, die durch separator
getrennt sind.An array that contains at most count
substrings from this instance that are delimited by separator
.
Hinweise
Wenn die Zeichenfolge bereits count
in 1-mal geteilt wurde, das Ende der Zeichenfolge jedoch noch nicht erreicht wurde, enthält die letzte Zeichenfolge im zurückgegebenen Array die verbleibende nachfolgende Teil Zeichenfolge dieser Instanz unverändert.If the string has already been split count
- 1 times, but the end of the string has not been reached, then the last string in the returned array will contain this instance's remaining trailing substring, untouched.
Gilt für:
Split(String[], Int32, StringSplitOptions)
Hier wird eine Zeichenfolge anhand von angegebenen Trennzeichenfolgen und optional von Optionen in die maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on specified delimiting strings and, optionally, options.
public:
cli::array <System::String ^> ^ Split(cli::array <System::String ^> ^ separator, int count, StringSplitOptions options);
public string[] Split (string[] separator, int count, StringSplitOptions options);
public string[] Split (string[]? separator, int count, StringSplitOptions options);
[System.Runtime.InteropServices.ComVisible(false)]
public string[] Split (string[] separator, int count, StringSplitOptions options);
member this.Split : string[] * int * StringSplitOptions -> string[]
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Split : string[] * int * StringSplitOptions -> string[]
Public Function Split (separator As String(), count As Integer, options As StringSplitOptions) As String()
Parameter
- separator
- String[]
Hierbei handelt es sich um die Zeichenfolgen, die die Teilzeichenfolgen in dieser Zeichenfolge trennen, ein leeres Array ohne Trennzeichen, oder null
.The strings that delimit the substrings in this string, an empty array that contains no delimiters, or null
.
- count
- Int32
Die maximale Anzahl der zurückzugebenden Teilzeichenfolgen.The maximum number of substrings to return.
- options
- StringSplitOptions
Dies ist eine bitweise Kombination der Enumerationswerte, die angibt, ob Teilzeichenfolgen zugeschnitten und leere Teilzeichenfolgen eingeschlossen werden sollen.A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.
Gibt zurück
- String[]
Ein Array, dessen Elemente die Teilzeichenfolgen in dieser Zeichenfolge enthält, die durch ein oder mehr Zeichenfolgen aus separator
getrennt sind.An array whose elements contain the substrings in this string that are delimited by one or more strings in separator
. Weitere Informationen finden Sie im Abschnitt "Hinweise".For more information, see the Remarks section.
- Attribute
Ausnahmen
count
ist ein negativer Wert.count
is negative.
options
entspricht keinem der StringSplitOptions-Werte.options
is not one of the StringSplitOptions values.
Beispiele
Im folgenden Beispiel wird die- StringSplitOptions Enumeration verwendet, um von der-Methode generierte Teil Zeichenfolgen einzuschließen oder auszuschließen Split .The following example uses the StringSplitOptions enumeration to include or exclude substrings generated by the Split method.
// This example demonstrates the String.Split(Char[], Boolean) and
// String.Split(Char[], Int32, Boolean) methods
using namespace System;
void Show( array<String^>^entries )
{
Console::WriteLine( "The return value contains these {0} elements:", entries->Length );
System::Collections::IEnumerator^ myEnum = entries->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ entry = safe_cast<String^>(myEnum->Current);
Console::Write( "<{0}>", entry );
}
Console::Write( "{0}{0}", Environment::NewLine );
}
int main()
{
String^ s = ",one,,,two,,,,,three,,";
array<Char>^sep = gcnew array<Char>{
','
};
array<String^>^result;
//
Console::WriteLine( "The original string is \"{0}\".", s );
Console::WriteLine( "The separation character is '{0}'.", sep[ 0 ] );
Console::WriteLine();
//
Console::WriteLine( "Split the string and return all elements:" );
result = s->Split( sep, StringSplitOptions::None );
Show( result );
//
Console::WriteLine( "Split the string and return all non-empty elements:" );
result = s->Split( sep, StringSplitOptions::RemoveEmptyEntries );
Show( result );
//
Console::WriteLine( "Split the string and return 2 elements:" );
result = s->Split( sep, 2, StringSplitOptions::None );
Show( result );
//
Console::WriteLine( "Split the string and return 2 non-empty elements:" );
result = s->Split( sep, 2, StringSplitOptions::RemoveEmptyEntries );
Show( result );
}
/*
This example produces the following results:
The original string is ",one,,,two,,,,,three,,".
The separation character is ','.
Split the string and return all elements:
The return value contains these 12 elements:
<><one><><><two><><><><><three><><>
Split the string and return all non-empty elements:
The return value contains these 3 elements:
<one><two><three>
Split the string and return 2 elements:
The return value contains these 2 elements:
<><one,,,two,,,,,three,,>
Split the string and return 2 non-empty elements:
The return value contains these 2 elements:
<one><,,two,,,,,three,,>
*/
// This example demonstrates the String.Split() methods that use
// the StringSplitOptions enumeration.
string s1 = ",ONE,,TWO,,,THREE,,";
string s2 = "[stop]" +
"ONE[stop][stop]" +
"TWO[stop][stop][stop]" +
"THREE[stop][stop]";
char[] charSeparators = new char[] { ',' };
string[] stringSeparators = new string[] { "[stop]" };
string[] result;
// ------------------------------------------------------------------------------
// Split a string delimited by characters.
// ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:\n");
// Display the original string and delimiter characters.
Console.WriteLine($"1a) The original string is \"{s1}\".");
Console.WriteLine($"The delimiter character is '{charSeparators[0]}'.\n");
// Split a string delimited by characters and return all elements.
Console.WriteLine("1b) Split a string delimited by characters and " +
"return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);
// Split a string delimited by characters and return all non-empty elements.
Console.WriteLine("1c) Split a string delimited by characters and " +
"return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Split the original string into the string and empty string before the
// delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("1d) Split a string delimited by characters and " +
"return 2 elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);
// Split the original string into the string after the delimiter and the
// remainder of the original string after the delimiter.
Console.WriteLine("1e) Split a string delimited by characters and " +
"return 2 non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// ------------------------------------------------------------------------------
// Split a string delimited by another string.
// ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:\n");
// Display the original string and delimiter string.
Console.WriteLine($"2a) The original string is \"{s2}\".");
Console.WriteLine($"The delimiter string is \"{stringSeparators[0]}\".\n");
// Split a string delimited by another string and return all elements.
Console.WriteLine("2b) Split a string delimited by another string and " +
"return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);
// Split the original string at the delimiter and return all non-empty elements.
Console.WriteLine("2c) Split a string delimited by another string and " +
"return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Split the original string into the empty string before the
// delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("2d) Split a string delimited by another string and " +
"return 2 elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);
// Split the original string into the string after the delimiter and the
// remainder of the original string after the delimiter.
Console.WriteLine("2e) Split a string delimited by another string and " +
"return 2 non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Display the array of separated strings using a local function
void Show(string[] entries)
{
Console.WriteLine($"The return value contains these {entries.Length} elements:");
foreach (string entry in entries)
{
Console.Write($"<{entry}>");
}
Console.Write("\n\n");
}
/*
This example produces the following results:
1) Split a string delimited by characters:
1a) The original string is ",ONE,,TWO,,,THREE,,".
The delimiter character is ','.
1b) Split a string delimited by characters and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>
1c) Split a string delimited by characters and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>
1d) Split a string delimited by characters and return 2 elements:
The return value contains these 2 elements:
<><ONE,,TWO,,,THREE,,>
1e) Split a string delimited by characters and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO,,,THREE,,>
2) Split a string delimited by another string:
2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
The delimiter string is "[stop]".
2b) Split a string delimited by another string and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>
2c) Split a string delimited by another string and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>
2d) Split a string delimited by another string and return 2 elements:
The return value contains these 2 elements:
<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
2e) Split a string delimited by another string and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
*/
Dim s1 As String = ",ONE,,TWO,,,THREE,,"
Dim s2 As String = "[stop]" &
"ONE[stop][stop]" &
"TWO[stop][stop][stop]" &
"THREE[stop][stop]"
Dim charSeparators() As Char = {","c}
Dim stringSeparators() As String = {"[stop]"}
Dim result() As String
' ------------------------------------------------------------------------------
' Split a string delimited by characters.
' ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:" & vbCrLf)
' Display the original string and delimiter characters.
Console.WriteLine("1a) The original string is ""{0}"".", s1)
Console.WriteLine("The delimiter character is '{0}'." & vbCrLf, charSeparators(0))
' Split a string delimited by characters and return all elements.
Console.WriteLine("1b) Split a string delimited by characters and " &
"return all elements:")
result = s1.Split(charSeparators, StringSplitOptions.None)
Show(result)
' Split a string delimited by characters and return all non-empty elements.
Console.WriteLine("1c) Split a string delimited by characters and " &
"return all non-empty elements:")
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' Split the original string into the string and empty string before the
' delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("1d) Split a string delimited by characters and " &
"return 2 elements:")
result = s1.Split(charSeparators, 2, StringSplitOptions.None)
Show(result)
' Split the original string into the string after the delimiter and the
' remainder of the original string after the delimiter.
Console.WriteLine("1e) Split a string delimited by characters and " &
"return 2 non-empty elements:")
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' ------------------------------------------------------------------------------
' Split a string delimited by another string.
' ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:" & vbCrLf)
' Display the original string and delimiter string.
Console.WriteLine("2a) The original string is ""{0}"".", s2)
Console.WriteLine("The delimiter string is ""{0}""." & vbCrLf, stringSeparators(0))
' Split a string delimited by another string and return all elements.
Console.WriteLine("2b) Split a string delimited by another string and " &
"return all elements:")
result = s2.Split(stringSeparators, StringSplitOptions.None)
Show(result)
' Split the original string at the delimiter and return all non-empty elements.
Console.WriteLine("2c) Split a string delimited by another string and " &
"return all non-empty elements:")
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' Split the original string into the empty string before the
' delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("2d) Split a string delimited by another string and " &
"return 2 elements:")
result = s2.Split(stringSeparators, 2, StringSplitOptions.None)
Show(result)
' Split the original string into the string after the delimiter and the
' remainder of the original string after the delimiter.
Console.WriteLine("2e) Split a string delimited by another string and " &
"return 2 non-empty elements:")
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Show(result)
End Sub
' Display the array of separated strings.
Public Shared Sub Show(ByVal entries() As String)
Console.WriteLine("The return value contains these {0} elements:", entries.Length)
Dim entry As String
For Each entry In entries
Console.Write("<{0}>", entry)
Next entry
Console.Write(vbCrLf & vbCrLf)
End Sub
'This example produces the following results:
'
'1) Split a string delimited by characters:
'
'1a) The original string is ",ONE,,TWO,,,THREE,,".
'The delimiter character is ','.
'
'1b) Split a string delimited by characters and return all elements:
'The return value contains these 9 elements:
'<><ONE><><TWO><><><THREE><><>
'
'1c) Split a string delimited by characters and return all non-empty elements:
'The return value contains these 3 elements:
'<ONE><TWO><THREE>
'
'1d) Split a string delimited by characters and return 2 elements:
'The return value contains these 2 elements:
'<><ONE,,TWO,,,THREE,,>
'
'1e) Split a string delimited by characters and return 2 non-empty elements:
'The return value contains these 2 elements:
'<ONE><TWO,,,THREE,,>
'
'2) Split a string delimited by another string:
'
'2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
'The delimiter string is "[stop]".
'
'2b) Split a string delimited by another string and return all elements:
'The return value contains these 9 elements:
'<><ONE><><TWO><><><THREE><><>
'
'2c) Split a string delimited by another string and return all non-empty elements:
'The return value contains these 3 elements:
'<ONE><TWO><THREE>
'
'2d) Split a string delimited by another string and return 2 elements:
'The return value contains these 2 elements:
'<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
'
'2e) Split a string delimited by another string and return 2 non-empty elements:
'The return value contains these 2 elements:
'<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
'
Hinweise
Trennzeichen-Zeichen folgen sind nicht in den Elementen des zurückgegebenen Arrays enthalten.Delimiter strings are not included in the elements of the returned array.
Wenn diese Instanz keine Zeichen folgen in enthält separator
oder der- count
Parameter 1 ist, besteht das zurückgegebene Array aus einem einzelnen-Element, das diese Instanz enthält.If this instance does not contain any of the strings in separator
, or the count
parameter is 1, the returned array consists of a single element that contains this instance.
Wenn der- separator
Parameter ist null
oder keine Zeichen enthält, wird davon ausgegangen, dass Leerzeichen als Trennzeichen verwendet werden.If the separator
parameter is null
or contains no characters, white-space characters are assumed to be the delimiters. Leerzeichen werden durch den Unicode-Standard definiert, und die Char.IsWhiteSpace Methode gibt zurück true
, wenn Sie an Sie übermittelt werden.White-space characters are defined by the Unicode standard and the Char.IsWhiteSpace method returns true
if they are passed to it.
Um null
für den- string[] separator
Parameter zu übergeben, müssen Sie den Typ der angeben, null
um den-Befehl von einigen anderen über Ladungen, z. b., zu unterscheiden Split(Char[], Int32, StringSplitOptions) .To pass null
for the string[] separator
parameter, you must indicate the type of the null
to disambiguate the call from some other overloads, such as Split(Char[], Int32, StringSplitOptions). Im folgenden Beispiel werden mehrere Möglichkeiten zum eindeutigen Identifizieren dieser Überladung veranschaulicht.The following example shows several ways to unambiguously identify this overload.
string phrase = "The quick brown fox";
_ = phrase.Split(default(string[]), 3, StringSplitOptions.RemoveEmptyEntries);
_ = phrase.Split((string[])null, 3, StringSplitOptions.RemoveEmptyEntries);
_ = phrase.Split(null as string[], 3, StringSplitOptions.RemoveEmptyEntries);
Dim phrase As String = "The quick brown fox"
Dim words() As String
words = phrase.Split(TryCast(Nothing, String()), 3,
StringSplitOptions.RemoveEmptyEntries)
words = phrase.Split(New String() {}, 3,
StringSplitOptions.RemoveEmptyEntries)
Wenn der- count
Parameter 0 (null) ist, oder wenn der options
-Parameter ist RemoveEmptyEntries und die Länge dieser Instanz 0 (null) ist, wird ein leeres Array zurückgegeben.If the count
parameter is zero, or the options
parameter is RemoveEmptyEntries and the length of this instance is zero, an empty array is returned.
Jedes Element von separator
definiert ein separates Trennzeichen, das aus einem oder mehreren Zeichen besteht.Each element of separator
defines a separate delimiter that consists of one or more characters. Wenn der options
-Parameter ist None und zwei Trennzeichen nebeneinander liegen oder ein Trennzeichen am Anfang oder Ende dieser Instanz gefunden wird, enthält das entsprechende Array Element Empty .If the options
parameter is None, and two delimiters are adjacent or a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty.
Wenn in dieser Instanz mehr als Teil Zeichenfolgen vorhanden sind count
, count
werden die ersten minus 1 Teil Zeichenfolgen in den ersten count
minus 1 Elementen des Rückgabewerts zurückgegeben, und die restlichen Zeichen in dieser Instanz werden im letzten Element des Rückgabewerts zurückgegeben.If there are more than count
substrings in this instance, the first count
minus 1 substrings are returned in the first count
minus 1 elements of the return value, and the remaining characters in this instance are returned in the last element of the return value.
Wenn count
größer als die Anzahl der Teil Zeichenfolgen ist, werden die verfügbaren Teil Zeichenfolgen zurückgegeben, und es wird keine Ausnahme ausgelöst.If count
is greater than the number of substrings, the available substrings are returned and no exception is thrown.
Das Trennzeichen ArrayThe separator array
Wenn eines der Elemente in separator
aus mehreren Zeichen besteht, gilt die gesamte Teil Zeichenfolge als Trennzeichen.If any of the elements in separator
consists of multiple characters, the entire substring is considered a delimiter. Wenn eines der Elemente in z. b separator
. "10" ist, wird versucht, die Zeichenfolge "This10is10a10string" aufzuteilen.For example, if one of the elements in separator
is "10", attempting to split the string "This10is10a10string." gibt dieses Array mit vier Elementen zurück: {"This", "is", "a", "String".returns this four-element array: { "This", "is", "a", "string." }.}.
Vergleichs DetailsComparison details
Die Split -Methode extrahiert die Teil Zeichenfolgen in dieser Zeichenfolge, die durch mindestens eine Zeichenfolge im- separator
Parameter getrennt sind, und gibt diese Teil Zeichenfolgen als Elemente eines Arrays zurück.The Split method extracts the substrings in this string that are delimited by one or more of the strings in the separator
parameter, and returns those substrings as elements of an array.
Die- Split Methode sucht nach Trennzeichen, indem Sie Vergleiche mithilfe von ordinalsortierungs Regeln für die Groß-und Kleinschreibung durchführtThe Split method looks for delimiters by performing comparisons using case-sensitive ordinal sort rules. Weitere Informationen zu Wort-, Zeichen folgen-und ordinalsortierungen finden Sie unter der- System.Globalization.CompareOptions Enumeration.For more information about word, string, and ordinal sorts, see the System.Globalization.CompareOptions enumeration.
Die- Split Methode ignoriert alle Elemente von, separator
deren Wert null
oder die leere Zeichenfolge ("") ist.The Split method ignores any element of separator
whose value is null
or the empty string ("").
Um mehrdeutige Ergebnisse zu vermeiden, wenn Zeichen folgen in separator
gemeinsame Zeichen aufweisen, wird die Split -Methode vom Anfang bis zum Ende des Werts der-Instanz fortgesetzt und entspricht dem ersten Element in, separator
das gleich einem Trennzeichen in der-Instanz ist.To avoid ambiguous results when strings in separator
have characters in common, the Split method proceeds from the beginning to the end of the value of the instance, and matches the first element in separator
that is equal to a delimiter in the instance. Die Reihenfolge, in der Teil Zeichenfolgen in der-Instanz gefunden werden, hat Vorrang vor der Reihenfolge der Elemente in separator
.The order in which substrings are encountered in the instance takes precedence over the order of elements in separator
.
Stellen Sie sich z. b. eine Instanz vor, deren Wert "abcdef" ist.For example, consider an instance whose value is "abcdef". Wenn das erste Element in separator
"EF" und das zweite Element "bcde" war, wäre das Ergebnis des Split-Vorgangs "a" und "f".If the first element in separator
was "ef" and the second element was "bcde", the result of the split operation would be "a" and "f". Dies liegt daran, dass die Teil Zeichenfolge in der-Instanz, "bcde", gefunden wird und mit einem Element in übereinstimmt, separator
bevor die Teil Zeichenfolge "f" gefunden wird.This is because the substring in the instance, "bcde", is encountered and matches an element in separator
before the substring "f" is encountered.
Wenn jedoch das erste Element von separator
"BCD" und das zweite Element "BC" war, wäre das Ergebnis des Split-Vorgangs "a" und "EF".However, if the first element of separator
was "bcd" and the second element was "bc", the result of the split operation would be "a" and "ef". Dies liegt daran, dass "BCD" das erste Trennzeichen in ist separator
, das mit einem Trennzeichen in der-Instanz übereinstimmt.This is because "bcd" is the first delimiter in separator
that matches a delimiter in the instance. Wenn die Reihenfolge der Trennzeichen umgekehrt wurde, sodass das erste Element "BC" und das zweite Element "BCD" war, wäre das Ergebnis "a" und "Def".If the order of the separators was reversed so the first element was "bc" and the second element was "bcd", the result would be "a" and "def".
Überlegungen zur LeistungPerformance considerations
Die Split -Methoden weisen Speicher für das zurückgegebene Array Objekt und ein- String Objekt für jedes Array Element zu.The Split methods allocate memory for the returned array object and a String object for each array element. Wenn Ihre Anwendung eine optimale Leistung erfordert oder wenn die Verwaltung der Speicher Belegung in der Anwendung wichtig ist, sollten Sie die IndexOf IndexOfAny -Methode oder die-Methode und optional die- Compare Methode verwenden, um eine Teil Zeichenfolge innerhalb einer Zeichenfolge zu suchen.If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method, and optionally the Compare method, to locate a substring within a string.
Wenn Sie eine Zeichenfolge an einem Trennzeichen aufteilen, verwenden Sie die- IndexOf Methode oder die- IndexOfAny Methode, um ein Trennzeichen in der Zeichenfolge zu suchen.If you are splitting a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. Wenn Sie eine Zeichenfolge in eine Trenn Zeichenfolge aufteilen, verwenden Sie die- IndexOf oder- IndexOfAny Methode, um das erste Zeichen der Trenn Zeichenfolge zu suchen.If you are splitting a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Verwenden Sie dann die- Compare Methode, um zu bestimmen, ob die Zeichen nach dem ersten Zeichen gleich den verbleibenden Zeichen der Trenn Zeichenfolge sind.Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
Wenn außerdem die gleichen Zeichen folgen zum Aufteilen von Zeichen folgen in mehreren Split Methoden aufrufen verwendet werden, sollten Sie in Erwägung gezogen werden, ein einzelnes Array zu erstellen und in jedem Methodenaufruf darauf zu verweisen.In addition, if the same set of characters is used to split strings in multiple Split method calls, consider creating a single array and referencing it in each method call. Dadurch wird der zusätzliche mehr Aufwand für die einzelnen Methodenaufrufe erheblich reduziert.This significantly reduces the additional overhead of each method call.
Hinweise für Aufrufer
In .NET Framework 3.5.NET Framework 3.5 und früheren Versionen Split(Char[]) separator
verwendet die-Methode null
einen etwas anderen Satz von Leerzeichen, um die Zeichenfolge zu teilen, wenn der Methode ein-Wert mit dem Wert oder keine Zeichen enthält Trim(Char[]) .In .NET Framework 3.5.NET Framework 3.5 and earlier versions, if the Split(Char[]) method is passed a separator
that is null
or contains no characters, the method uses a slightly different set of white-space characters to split the string than the Trim(Char[]) method does to trim the string. Beginnend mit .NET Framework 4 verwenden beide Methoden einen identischen Satz von Unicode-Leerzeichen.Starting with .NET Framework 4, both methods use an identical set of Unicode white-space characters.
Gilt für:
Split(String[], StringSplitOptions)
Hier wird eine Zeichenfolge anhand einer angegebenen Trennzeichenfolge und optional von Optionen in Teilzeichenfolgen unterteilt.Splits a string into substrings based on a specified delimiting string and, optionally, options.
public:
cli::array <System::String ^> ^ Split(cli::array <System::String ^> ^ separator, StringSplitOptions options);
public string[] Split (string[] separator, StringSplitOptions options);
public string[] Split (string[]? separator, StringSplitOptions options);
[System.Runtime.InteropServices.ComVisible(false)]
public string[] Split (string[] separator, StringSplitOptions options);
member this.Split : string[] * StringSplitOptions -> string[]
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Split : string[] * StringSplitOptions -> string[]
Public Function Split (separator As String(), options As StringSplitOptions) As String()
Parameter
- separator
- String[]
Hierbei handelt es sich um ein Array von Zeichenfolgen, die die Teilzeichenfolgen in dieser Zeichenfolge trennen, ein leeres Array ohne Trennzeichen, oder null
.An array of strings that delimit the substrings in this string, an empty array that contains no delimiters, or null
.
- options
- StringSplitOptions
Dies ist eine bitweise Kombination der Enumerationswerte, die angibt, ob Teilzeichenfolgen zugeschnitten und leere Teilzeichenfolgen eingeschlossen werden sollen.A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.
Gibt zurück
- String[]
Ein Array, dessen Elemente die Teilzeichenfolgen in dieser Zeichenfolge enthält, die durch ein oder mehr Zeichenfolgen aus separator
getrennt sind.An array whose elements contain the substrings in this string that are delimited by one or more strings in separator
. Weitere Informationen finden Sie im Abschnitt "Hinweise".For more information, see the Remarks section.
- Attribute
Ausnahmen
options
entspricht keinem der StringSplitOptions-Werte.options
is not one of the StringSplitOptions values.
Beispiele
Im folgenden Beispiel wird der Unterschied in den Arrays veranschaulicht, die durch Aufrufen der-Methode einer Zeichenfolge zurückgegeben werden String.Split(String[], StringSplitOptions) , deren- options
Parameter gleich StringSplitOptions.None und ist StringSplitOptions.RemoveEmptyEntries .The following example illustrates the difference in the arrays returned by calling a string's String.Split(String[], StringSplitOptions) method with its options
parameter equal to StringSplitOptions.None and StringSplitOptions.RemoveEmptyEntries.
string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] { "[stop]" };
string[] result;
// Display the original string and delimiter string.
Console.WriteLine($"Splitting the string:\n \"{source}\".");
Console.WriteLine();
Console.WriteLine($"Using the delimiter string:\n \"{stringSeparators[0]}\"");
Console.WriteLine();
// Split a string delimited by another string and return all elements.
result = source.Split(stringSeparators, StringSplitOptions.None);
Console.WriteLine($"Result including all elements ({result.Length} elements):");
Console.Write(" ");
foreach (string s in result)
{
Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
Console.WriteLine();
Console.WriteLine();
// Split delimited by another string and return all non-empty elements.
result = source.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"Result including non-empty elements ({result.Length} elements):");
Console.Write(" ");
foreach (string s in result)
{
Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
Console.WriteLine();
// The example displays the following output:
// Splitting the string:
// "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
//
// Using the delimiter string:
// "[stop]"
//
// Result including all elements (9 elements):
// '<>' 'ONE' '<>' 'TWO' '<>' '<>' 'THREE' '<>' '<>'
//
// Result including non-empty elements (3 elements):
// 'ONE' 'TWO' 'THREE'
Dim source As String = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]"
Dim stringSeparators() As String = {"[stop]"}
Dim result() As String
' Display the original string and delimiter string.
Console.WriteLine("Splitting the string:{0} '{1}'.", vbCrLf, source)
Console.WriteLine()
Console.WriteLine("Using the delimiter string:{0} '{1}'.",
vbCrLf, stringSeparators(0))
Console.WriteLine()
' Split a string delimited by another string and return all elements.
result = source.Split(stringSeparators, StringSplitOptions.None)
Console.WriteLine("Result including all elements ({0} elements):",
result.Length)
Console.Write(" ")
For Each s As String In result
Console.Write("'{0}' ", IIf(String.IsNullOrEmpty(s), "<>", s))
Next
Console.WriteLine()
Console.WriteLine()
' Split delimited by another string and return all non-empty elements.
result = source.Split(stringSeparators,
StringSplitOptions.RemoveEmptyEntries)
Console.WriteLine("Result including non-empty elements ({0} elements):",
result.Length)
Console.Write(" ")
For Each s As String In result
Console.Write("'{0}' ", IIf(String.IsNullOrEmpty(s), "<>", s))
Next
Console.WriteLine()
' The example displays the following output:
' Splitting the string:
' "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
'
' Using the delimiter string:
' "[stop]"
'
' Result including all elements (9 elements):
' '<>' 'ONE' '<>' 'TWO' '<>' '<>' 'THREE' '<>' '<>'
'
' Result including non-empty elements (3 elements):
' 'ONE' 'TWO' 'THREE'
Im folgenden Beispiel wird ein Array von Trennzeichen definiert, das Interpunktions-und Leerzeichen enthält.The following example defines an array of separators that include punctuation and white-space characters. Wenn Sie dieses Array zusammen mit einem Wert von an die-Methode übergeben, wird StringSplitOptions.RemoveEmptyEntries Split(String[], StringSplitOptions) ein Array zurückgegeben, das aus den einzelnen Wörtern aus der Zeichenfolge besteht.Passing this array along with a value of StringSplitOptions.RemoveEmptyEntries to the Split(String[], StringSplitOptions) method returns an array that consists of the individual words from the string.
string[] separators = { ",", ".", "!", "?", ";", ":", " " };
string value = "The handsome, energetic, young dog was playing with his smaller, more lethargic litter mate.";
string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (var word in words)
Console.WriteLine(word);
// The example displays the following output:
// The
// handsome
// energetic
// young
// dog
// was
// playing
// with
// his
// smaller
// more
// lethargic
// litter
// mate
Dim separators() As String = {",", ".", "!", "?", ";", ":", " "}
Dim value As String = "The handsome, energetic, young dog was playing with his smaller, more lethargic litter mate."
Dim words() As String = value.Split(separators, StringSplitOptions.RemoveEmptyEntries)
For Each word In words
Console.WriteLine(word)
Next
End Sub
' The example displays the following output:
'
' The
' handsome
' energetic
' young
' dog
' was
' playing
' with
' his
' smaller
' more
' lethargic
' litter
' mate
Beachten Sie, dass die-Methode aufgerufen wird und das- options
Argument auf festgelegt ist StringSplitOptions.RemoveEmptyEntries .Note that the method is called with the options
argument set to StringSplitOptions.RemoveEmptyEntries. Dadurch wird verhindert, dass das zurückgegebene Array String.Empty Werte einschließt, die leere Teil Zeichenfolgen-Übereinstimmungen zwischen Interpunktions Zeichen und Leerzeichen darstellen.This prevents the returned array from including String.Empty values that represent empty substring matches between punctuation marks and white-space characters.
Hinweise
Wenn eine Zeichenfolge durch einen bekannten Zeichen folgen Satz getrennt ist, können Sie die- Split Methode verwenden, um Sie in Teil Zeichenfolgen zu trennen.When a string is delimited by a known set of strings, you can use the Split method to separate it into substrings.
Trennzeichen-Zeichen folgen sind nicht in den Elementen des zurückgegebenen Arrays enthalten.Delimiter strings are not included in the elements of the returned array. Wenn das Array z. b. separator
die Zeichenfolge "--" enthält und der Wert der aktuellen Zeichen folgen Instanz "AA--BB--CC" ist, gibt die Methode ein Array zurück, das drei Elemente enthält: "AA", "BB" und "CC".For example, if the separator
array includes the string "--" and the value of the current string instance is "aa--bb--cc", the method returns an array that contains three elements: "aa", "bb", and "cc".
Wenn diese Instanz keine Zeichen folgen in enthält separator
, besteht das zurückgegebene Array aus einem einzelnen Element, das diese Instanz enthält.If this instance does not contain any of the strings in separator
, the returned array consists of a single element that contains this instance.
Wenn der options
-Parameter RemoveEmptyEntries und die Länge dieser Instanz 0 (null) ist, gibt die Methode ein leeres Array zurück.If the options
parameter is RemoveEmptyEntries and the length of this instance is zero, the method returns an empty array.
Jedes Element von separator
definiert ein separates Trennzeichen, das aus einem oder mehreren Zeichen besteht.Each element of separator
defines a separate delimiter that consists of one or more characters. Wenn das options
-Argument ist None und zwei Trennzeichen nebeneinander liegen oder ein Trennzeichen am Anfang oder Ende dieser Instanz gefunden wird, enthält das entsprechende Array Element String.Empty .If the options
argument is None, and two delimiters are adjacent or a delimiter is found at the beginning or end of this instance, the corresponding array element contains String.Empty. Wenn z. b. separator
zwei Elemente enthält: "-" und " _ ", ist der Wert der Zeichen folgen Instanz "- _ AA- _ ", und der Wert des- options
Arguments ist None , die Methode gibt ein Zeichen folgen Array mit den folgenden fünf Elementen zurück:For example, if separator
includes two elements, "-" and "_", the value of the string instance is "-_aa-_", and the value of the options
argument is None, the method returns a string array with the following five elements:
String.Emptystellt die leere Zeichenfolge dar, die der "-"-Teil Zeichenfolge bei Index 0 vorangestellt ist.String.Empty, which represents the empty string that precedes the "-" substring at index 0.
String.Empty, die die leere Zeichenfolge zwischen der "-"-Teil Zeichenfolge bei Index 0 und der Teil Zeichenfolge "" bei Index 1 darstellt.String.Empty, which represents the empty string between the "-" substring at index 0 and the "" substring at index 1.
"AA"."aa".
String.Emptystellt die leere Zeichenfolge dar, die auf die Teil Zeichenfolge "-" bei Index 4 folgt.String.Empty, which represents the empty string that follows the "-" substring at index 4.
String.Emptystellt die leere Zeichenfolge dar, die auf die Teil Zeichenfolge "" bei Index 5 folgt.String.Empty, which represents the empty string that follows the "" substring at index 5.
Das Trennzeichen ArrayThe separator array
Wenn eines der Elemente in separator
aus mehreren Zeichen besteht, gilt die gesamte Teil Zeichenfolge als Trennzeichen.If any of the elements in separator
consists of multiple characters, the entire substring is considered a delimiter. Wenn eines der Elemente in z. b separator
. "10" ist, wird versucht, die Zeichenfolge "This10is10a10string" aufzuteilen.For example, if one of the elements in separator
is "10", attempting to split the string "This10is10a10string." Gibt das folgende Array von vier Elementen zurück: {"This", "is", "a", "String".returns the following four-element array: { "This", "is", "a", "string." }.}.
Wenn der- separator
Parameter ist null
oder keine nicht leeren Zeichen folgen enthält, wird davon ausgegangen, dass Leerzeichen als Trennzeichen verwendet werden.If the separator
parameter is null
or contains no non-empty strings, white-space characters are assumed to be the delimiters. Leerzeichen werden durch den Unicode-Standard definiert, und die Char.IsWhiteSpace Methode gibt zurück true
, wenn Sie an Sie übermittelt werden.White-space characters are defined by the Unicode standard and the Char.IsWhiteSpace method returns true
if they are passed to it.
Um null
für den- string[] separator
Parameter zu übergeben, müssen Sie den Typ der angeben, null
um den-Befehl von einigen anderen über Ladungen, z. b., zu unterscheiden Split(Char[], StringSplitOptions) .To pass null
for the string[] separator
parameter, you must indicate the type of the null
to disambiguate the call from some other overloads, such as Split(Char[], StringSplitOptions). Im folgenden Beispiel werden mehrere Möglichkeiten zum eindeutigen Identifizieren dieser Überladung veranschaulicht.The following example shows several ways to unambiguously identify this overload.
string phrase = "The quick brown fox";
_ = phrase.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
_ = phrase.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);
_ = phrase.Split(null as string[], StringSplitOptions.RemoveEmptyEntries);
Dim phrase As String = "The quick brown fox"
Dim words() As String
words = phrase.Split(TryCast(Nothing, String()),
StringSplitOptions.RemoveEmptyEntries)
words = phrase.Split(New String() {},
StringSplitOptions.RemoveEmptyEntries)
Vergleichs DetailsComparison details
Die Split -Methode extrahiert die Teil Zeichenfolgen in dieser Zeichenfolge, die durch mindestens eine Zeichenfolge im- separator
Parameter getrennt sind, und gibt diese Teil Zeichenfolgen als Elemente eines Arrays zurück.The Split method extracts the substrings in this string that are delimited by one or more of the strings in the separator
parameter, and returns those substrings as elements of an array.
Die- Split Methode sucht nach Trennzeichen, indem Sie Vergleiche mithilfe von ordinalsortierungs Regeln für die Groß-und Kleinschreibung durchführtThe Split method looks for delimiters by performing comparisons using case-sensitive ordinal sort rules. Weitere Informationen zu Wort-, Zeichen folgen-und ordinalsortierungen finden Sie unter der- System.Globalization.CompareOptions Enumeration.For more information about word, string, and ordinal sorts, see the System.Globalization.CompareOptions enumeration.
Die- Split Methode ignoriert alle Elemente von, separator
deren Wert null
oder die leere Zeichenfolge ("") ist.The Split method ignores any element of separator
whose value is null
or the empty string ("").
Um mehrdeutige Ergebnisse zu vermeiden, wenn Zeichen folgen in separator
gemeinsame Zeichen aufweisen, wird der Split Vorgang vom Anfang bis zum Ende des Werts der-Instanz fortgesetzt und entspricht dem ersten Element in, separator
das gleich einem Trennzeichen in der-Instanz ist.To avoid ambiguous results when strings in separator
have characters in common, the Split operation proceeds from the beginning to the end of the value of the instance, and matches the first element in separator
that is equal to a delimiter in the instance. Die Reihenfolge, in der Teil Zeichenfolgen in der-Instanz gefunden werden, hat Vorrang vor der Reihenfolge der Elemente in separator
.The order in which substrings are encountered in the instance takes precedence over the order of elements in separator
.
Stellen Sie sich z. b. eine Instanz vor, deren Wert "abcdef" ist.For example, consider an instance whose value is "abcdef". Wenn das erste Element in separator
"EF" und das zweite Element "bcde" war, wäre das Ergebnis des Split-Vorgangs ein Zeichen folgen Array, das zwei Elemente enthält: "a" und "f".If the first element in separator
was "ef" and the second element was "bcde", the result of the split operation would be a string array that contains two elements, "a" and "f". Dies liegt daran, dass die Teil Zeichenfolge in der-Instanz, "bcde", gefunden wird und mit einem Element in übereinstimmt, separator
bevor die Teil Zeichenfolge "f" gefunden wird.This is because the substring in the instance, "bcde", is encountered and matches an element in separator
before the substring "f" is encountered.
Wenn jedoch das erste Element von separator
"BCD" und das zweite Element "BC" war, wäre das Ergebnis des Split-Vorgangs ein Zeichen folgen Array, das zwei Elemente enthält: "a" und "EF".However, if the first element of separator
was "bcd" and the second element was "bc", the result of the split operation would be a string array that contains two elements, "a" and "ef". Dies liegt daran, dass "BCD" das erste Trennzeichen in ist separator
, das mit einem Trennzeichen in der-Instanz übereinstimmt.This is because "bcd" is the first delimiter in separator
that matches a delimiter in the instance. Wenn die Reihenfolge der Trennzeichen umgekehrt wurde, sodass das erste Element "BC" und das zweite Element "BCD" war, wäre das Ergebnis ein Zeichen folgen Array, das zwei Elemente enthält: "a" und "Def".If the order of the separators was reversed so the first element was "bc" and the second element was "bcd", the result would be a string array that contains two elements, "a" and "def".
Überlegungen zur LeistungPerformance considerations
Die Split -Methoden weisen Speicher für das zurückgegebene Array Objekt und ein- String Objekt für jedes Array Element zu.The Split methods allocate memory for the returned array object and a String object for each array element. Wenn Ihre Anwendung eine optimale Leistung erfordert oder wenn die Verwaltung der Speicher Belegung in der Anwendung wichtig ist, sollten Sie die IndexOf IndexOfAny -Methode oder die-Methode und optional die- Compare Methode verwenden, um eine Teil Zeichenfolge innerhalb einer Zeichenfolge zu suchen.If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method, and optionally the Compare method, to locate a substring within a string.
Wenn Sie eine Zeichenfolge an einem Trennzeichen aufteilen, verwenden Sie die- IndexOf Methode oder die- IndexOfAny Methode, um ein Trennzeichen in der Zeichenfolge zu suchen.If you are splitting a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. Wenn Sie eine Zeichenfolge in eine Trenn Zeichenfolge aufteilen, verwenden Sie die- IndexOf oder- IndexOfAny Methode, um das erste Zeichen der Trenn Zeichenfolge zu suchen.If you are splitting a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Verwenden Sie dann die- Compare Methode, um zu bestimmen, ob die Zeichen nach dem ersten Zeichen gleich den verbleibenden Zeichen der Trenn Zeichenfolge sind.Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
Wenn außerdem die gleichen Zeichen folgen zum Aufteilen von Zeichen folgen in mehreren Split Methoden aufrufen verwendet werden, sollten Sie in Erwägung gezogen werden, ein einzelnes Array zu erstellen und in jedem Methodenaufruf darauf zu verweisen.In addition, if the same set of characters is used to split strings in multiple Split method calls, consider creating a single array and referencing it in each method call. Dadurch wird der zusätzliche mehr Aufwand für die einzelnen Methodenaufrufe erheblich reduziert.This significantly reduces the additional overhead of each method call.
Hinweise für Aufrufer
In .NET Framework 3.5.NET Framework 3.5 und früheren Versionen Split(Char[]) separator
verwendet die-Methode null
einen etwas anderen Satz von Leerzeichen, um die Zeichenfolge zu teilen, wenn der Methode ein-Wert mit dem Wert oder keine Zeichen enthält Trim(Char[]) .In .NET Framework 3.5.NET Framework 3.5 and earlier versions, if the Split(Char[]) method is passed a separator
that is null
or contains no characters, the method uses a slightly different set of white-space characters to split the string than the Trim(Char[]) method does to trim the string. Beginnend mit .NET Framework 4 verwenden beide Methoden einen identischen Satz von Unicode-Leerzeichen.Starting with .NET Framework 4, both methods use an identical set of Unicode white-space characters.
Gilt für:
Split(String, Int32, StringSplitOptions)
Hier wird eine Zeichenfolge anhand einer angegebenen Trennzeichenfolge und optional von Optionen in die maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on a specified delimiting string and, optionally, options.
public string[] Split (string? separator, int count, StringSplitOptions options = System.StringSplitOptions.None);
public string[] Split (string separator, int count, StringSplitOptions options = System.StringSplitOptions.None);
member this.Split : string * int * StringSplitOptions -> string[]
Public Function Split (separator As String, count As Integer, Optional options As StringSplitOptions = System.StringSplitOptions.None) As String()
Parameter
- separator
- String
Eine Zeichenfolge, die die Teilzeichenfolgen in dieser Instanz trennt.A string that delimits the substrings in this instance.
- count
- Int32
Die maximale Anzahl der im Array erwarteten Elemente.The maximum number of elements expected in the array.
- options
- StringSplitOptions
Dies ist eine bitweise Kombination der Enumerationswerte, die angibt, ob Teilzeichenfolgen zugeschnitten und leere Teilzeichenfolgen eingeschlossen werden sollen.A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.
Gibt zurück
- String[]
Ein Array, das maximal count
Teilzeichenfolgen von dieser Instanz enthält, die durch separator
getrennt sind.An array that contains at most count
substrings from this instance that are delimited by separator
.
Hinweise
Wenn die Zeichenfolge bereits count
in 1-mal geteilt wurde, das Ende der Zeichenfolge jedoch noch nicht erreicht wurde, enthält die letzte Zeichenfolge im zurückgegebenen Array die verbleibende nachfolgende Teil Zeichenfolge dieser Instanz unverändert.If the string has already been split count
- 1 times, but the end of the string has not been reached, then the last string in the returned array will contain this instance's remaining trailing substring, untouched.
Gilt für:
Split(Char[], StringSplitOptions)
Hier wird eine Zeichenfolge anhand der angegebenen Trennzeichen und der Optionen in Teilzeichenfolgen unterteilt.Splits a string into substrings based on specified delimiting characters and options.
public:
cli::array <System::String ^> ^ Split(cli::array <char> ^ separator, StringSplitOptions options);
public string[] Split (char[] separator, StringSplitOptions options);
public string[] Split (char[]? separator, StringSplitOptions options);
[System.Runtime.InteropServices.ComVisible(false)]
public string[] Split (char[] separator, StringSplitOptions options);
member this.Split : char[] * StringSplitOptions -> string[]
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Split : char[] * StringSplitOptions -> string[]
Public Function Split (separator As Char(), options As StringSplitOptions) As String()
Parameter
- separator
- Char[]
Hierbei handelt es sich um ein Array von Zeichen, die die Teilzeichenfolgen dieser Zeichenfolge trennen, ein leeres Array ohne Trennzeichen oder null
.An array of characters that delimit the substrings in this string, an empty array that contains no delimiters, or null
.
- options
- StringSplitOptions
Dies ist eine bitweise Kombination der Enumerationswerte, die angibt, ob Teilzeichenfolgen zugeschnitten und leere Teilzeichenfolgen eingeschlossen werden sollen.A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.
Gibt zurück
- String[]
Ein Array, dessen Elemente die Teilzeichenfolgen in dieser Zeichenfolge enthält, die durch ein oder mehr Zeichen aus separator
getrennt sind.An array whose elements contain the substrings in this string that are delimited by one or more characters in separator
. Weitere Informationen finden Sie im Abschnitt "Hinweise".For more information, see the Remarks section.
- Attribute
Ausnahmen
options
entspricht keinem der StringSplitOptions-Werte.options
is not one of the StringSplitOptions values.
Beispiele
Im folgenden Beispiel wird die- StringSplitOptions Enumeration verwendet, um von der-Methode generierte Teil Zeichenfolgen einzuschließen oder auszuschließen Split .The following example uses the StringSplitOptions enumeration to include or exclude substrings generated by the Split method.
// This example demonstrates the String.Split(Char[], Boolean) and
// String.Split(Char[], Int32, Boolean) methods
using namespace System;
void Show( array<String^>^entries )
{
Console::WriteLine( "The return value contains these {0} elements:", entries->Length );
System::Collections::IEnumerator^ myEnum = entries->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ entry = safe_cast<String^>(myEnum->Current);
Console::Write( "<{0}>", entry );
}
Console::Write( "{0}{0}", Environment::NewLine );
}
int main()
{
String^ s = ",one,,,two,,,,,three,,";
array<Char>^sep = gcnew array<Char>{
','
};
array<String^>^result;
//
Console::WriteLine( "The original string is \"{0}\".", s );
Console::WriteLine( "The separation character is '{0}'.", sep[ 0 ] );
Console::WriteLine();
//
Console::WriteLine( "Split the string and return all elements:" );
result = s->Split( sep, StringSplitOptions::None );
Show( result );
//
Console::WriteLine( "Split the string and return all non-empty elements:" );
result = s->Split( sep, StringSplitOptions::RemoveEmptyEntries );
Show( result );
//
Console::WriteLine( "Split the string and return 2 elements:" );
result = s->Split( sep, 2, StringSplitOptions::None );
Show( result );
//
Console::WriteLine( "Split the string and return 2 non-empty elements:" );
result = s->Split( sep, 2, StringSplitOptions::RemoveEmptyEntries );
Show( result );
}
/*
This example produces the following results:
The original string is ",one,,,two,,,,,three,,".
The separation character is ','.
Split the string and return all elements:
The return value contains these 12 elements:
<><one><><><two><><><><><three><><>
Split the string and return all non-empty elements:
The return value contains these 3 elements:
<one><two><three>
Split the string and return 2 elements:
The return value contains these 2 elements:
<><one,,,two,,,,,three,,>
Split the string and return 2 non-empty elements:
The return value contains these 2 elements:
<one><,,two,,,,,three,,>
*/
// This example demonstrates the String.Split() methods that use
// the StringSplitOptions enumeration.
string s1 = ",ONE,,TWO,,,THREE,,";
string s2 = "[stop]" +
"ONE[stop][stop]" +
"TWO[stop][stop][stop]" +
"THREE[stop][stop]";
char[] charSeparators = new char[] { ',' };
string[] stringSeparators = new string[] { "[stop]" };
string[] result;
// ------------------------------------------------------------------------------
// Split a string delimited by characters.
// ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:\n");
// Display the original string and delimiter characters.
Console.WriteLine($"1a) The original string is \"{s1}\".");
Console.WriteLine($"The delimiter character is '{charSeparators[0]}'.\n");
// Split a string delimited by characters and return all elements.
Console.WriteLine("1b) Split a string delimited by characters and " +
"return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);
// Split a string delimited by characters and return all non-empty elements.
Console.WriteLine("1c) Split a string delimited by characters and " +
"return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Split the original string into the string and empty string before the
// delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("1d) Split a string delimited by characters and " +
"return 2 elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);
// Split the original string into the string after the delimiter and the
// remainder of the original string after the delimiter.
Console.WriteLine("1e) Split a string delimited by characters and " +
"return 2 non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// ------------------------------------------------------------------------------
// Split a string delimited by another string.
// ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:\n");
// Display the original string and delimiter string.
Console.WriteLine($"2a) The original string is \"{s2}\".");
Console.WriteLine($"The delimiter string is \"{stringSeparators[0]}\".\n");
// Split a string delimited by another string and return all elements.
Console.WriteLine("2b) Split a string delimited by another string and " +
"return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);
// Split the original string at the delimiter and return all non-empty elements.
Console.WriteLine("2c) Split a string delimited by another string and " +
"return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Split the original string into the empty string before the
// delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("2d) Split a string delimited by another string and " +
"return 2 elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);
// Split the original string into the string after the delimiter and the
// remainder of the original string after the delimiter.
Console.WriteLine("2e) Split a string delimited by another string and " +
"return 2 non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
// Display the array of separated strings using a local function
void Show(string[] entries)
{
Console.WriteLine($"The return value contains these {entries.Length} elements:");
foreach (string entry in entries)
{
Console.Write($"<{entry}>");
}
Console.Write("\n\n");
}
/*
This example produces the following results:
1) Split a string delimited by characters:
1a) The original string is ",ONE,,TWO,,,THREE,,".
The delimiter character is ','.
1b) Split a string delimited by characters and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>
1c) Split a string delimited by characters and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>
1d) Split a string delimited by characters and return 2 elements:
The return value contains these 2 elements:
<><ONE,,TWO,,,THREE,,>
1e) Split a string delimited by characters and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO,,,THREE,,>
2) Split a string delimited by another string:
2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
The delimiter string is "[stop]".
2b) Split a string delimited by another string and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>
2c) Split a string delimited by another string and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>
2d) Split a string delimited by another string and return 2 elements:
The return value contains these 2 elements:
<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
2e) Split a string delimited by another string and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
*/
Dim s1 As String = ",ONE,,TWO,,,THREE,,"
Dim s2 As String = "[stop]" &
"ONE[stop][stop]" &
"TWO[stop][stop][stop]" &
"THREE[stop][stop]"
Dim charSeparators() As Char = {","c}
Dim stringSeparators() As String = {"[stop]"}
Dim result() As String
' ------------------------------------------------------------------------------
' Split a string delimited by characters.
' ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:" & vbCrLf)
' Display the original string and delimiter characters.
Console.WriteLine("1a) The original string is ""{0}"".", s1)
Console.WriteLine("The delimiter character is '{0}'." & vbCrLf, charSeparators(0))
' Split a string delimited by characters and return all elements.
Console.WriteLine("1b) Split a string delimited by characters and " &
"return all elements:")
result = s1.Split(charSeparators, StringSplitOptions.None)
Show(result)
' Split a string delimited by characters and return all non-empty elements.
Console.WriteLine("1c) Split a string delimited by characters and " &
"return all non-empty elements:")
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' Split the original string into the string and empty string before the
' delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("1d) Split a string delimited by characters and " &
"return 2 elements:")
result = s1.Split(charSeparators, 2, StringSplitOptions.None)
Show(result)
' Split the original string into the string after the delimiter and the
' remainder of the original string after the delimiter.
Console.WriteLine("1e) Split a string delimited by characters and " &
"return 2 non-empty elements:")
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' ------------------------------------------------------------------------------
' Split a string delimited by another string.
' ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:" & vbCrLf)
' Display the original string and delimiter string.
Console.WriteLine("2a) The original string is ""{0}"".", s2)
Console.WriteLine("The delimiter string is ""{0}""." & vbCrLf, stringSeparators(0))
' Split a string delimited by another string and return all elements.
Console.WriteLine("2b) Split a string delimited by another string and " &
"return all elements:")
result = s2.Split(stringSeparators, StringSplitOptions.None)
Show(result)
' Split the original string at the delimiter and return all non-empty elements.
Console.WriteLine("2c) Split a string delimited by another string and " &
"return all non-empty elements:")
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' Split the original string into the empty string before the
' delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("2d) Split a string delimited by another string and " &
"return 2 elements:")
result = s2.Split(stringSeparators, 2, StringSplitOptions.None)
Show(result)
' Split the original string into the string after the delimiter and the
' remainder of the original string after the delimiter.
Console.WriteLine("2e) Split a string delimited by another string and " &
"return 2 non-empty elements:")
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Show(result)
End Sub
' Display the array of separated strings.
Public Shared Sub Show(ByVal entries() As String)
Console.WriteLine("The return value contains these {0} elements:", entries.Length)
Dim entry As String
For Each entry In entries
Console.Write("<{0}>", entry)
Next entry
Console.Write(vbCrLf & vbCrLf)
End Sub
'This example produces the following results:
'
'1) Split a string delimited by characters:
'
'1a) The original string is ",ONE,,TWO,,,THREE,,".
'The delimiter character is ','.
'
'1b) Split a string delimited by characters and return all elements:
'The return value contains these 9 elements:
'<><ONE><><TWO><><><THREE><><>
'
'1c) Split a string delimited by characters and return all non-empty elements:
'The return value contains these 3 elements:
'<ONE><TWO><THREE>
'
'1d) Split a string delimited by characters and return 2 elements:
'The return value contains these 2 elements:
'<><ONE,,TWO,,,THREE,,>
'
'1e) Split a string delimited by characters and return 2 non-empty elements:
'The return value contains these 2 elements:
'<ONE><TWO,,,THREE,,>
'
'2) Split a string delimited by another string:
'
'2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
'The delimiter string is "[stop]".
'
'2b) Split a string delimited by another string and return all elements:
'The return value contains these 9 elements:
'<><ONE><><TWO><><><THREE><><>
'
'2c) Split a string delimited by another string and return all non-empty elements:
'The return value contains these 3 elements:
'<ONE><TWO><THREE>
'
'2d) Split a string delimited by another string and return 2 elements:
'The return value contains these 2 elements:
'<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
'
'2e) Split a string delimited by another string and return 2 non-empty elements:
'The return value contains these 2 elements:
'<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
'
Hinweise
Trennzeichen (die Zeichen im separator
Array) sind nicht in den Elementen des zurückgegebenen Arrays enthalten.Delimiter characters (the characters in the separator
array) are not included in the elements of the returned array. Wenn das Array z. b. separator
das Zeichen "-" enthält und der Wert der aktuellen Zeichen folgen Instanz "AA-BB-CC" ist, gibt die Methode ein Array zurück, das drei Elemente enthält: "AA", "BB" und "CC".For example, if the separator
array includes the character "-" and the value of the current string instance is "aa-bb-cc", the method returns an array that contains three elements: "aa", "bb", and "cc".
Wenn diese Instanz keines der Zeichen in enthält separator
, besteht das zurückgegebene Array aus einem einzelnen Element, das diese Instanz enthält.If this instance does not contain any of the characters in separator
, the returned array consists of a single element that contains this instance.
Wenn der options
-Parameter RemoveEmptyEntries und die Länge dieser Instanz 0 (null) ist, gibt die Methode ein leeres Array zurück.If the options
parameter is RemoveEmptyEntries and the length of this instance is zero, the method returns an empty array.
Jedes Element von separator
definiert ein separates Trennzeichen, das aus einem einzelnen Zeichen besteht.Each element of separator
defines a separate delimiter that consists of a single character. Wenn das options
-Argument ist None und zwei Trennzeichen nebeneinander liegen oder ein Trennzeichen am Anfang oder Ende dieser Instanz gefunden wird, enthält das entsprechende Array Element String.Empty .If the options
argument is None, and two delimiters are adjacent or a delimiter is found at the beginning or end of this instance, the corresponding array element contains String.Empty. Wenn z. b. separator
zwei-Elemente enthält, '-'
und '_'
der Wert der Zeichen folgen Instanz "- _ AA- _ " und der Wert des- options
Arguments ist None , gibt die Methode ein Zeichen folgen Array mit den folgenden fünf Elementen zurück:For example, if separator
includes two elements, '-'
and '_'
, the value of the string instance is "-_aa-_", and the value of the options
argument is None, the method returns a string array with the following five elements:
String.Emptystellt die leere Zeichenfolge dar, die dem Zeichen "-" bei Index 0 vorangestellt ist.String.Empty, which represents the empty string that precedes the "-" character at index 0.
String.Empty, die die leere Zeichenfolge zwischen dem Zeichen "-" bei Index 0 und dem Zeichen "" bei Index 1 darstellt.String.Empty, which represents the empty string between the "-" character at index 0 and the "" character at index 1.
"AA"."aa".
String.Empty, die die leere Zeichenfolge darstellt, die auf das Zeichen "-" am Index 4 folgt.String.Empty, which represents the empty string that follows the "-" character at index 4.
String.Empty, die die leere Zeichenfolge darstellt, die auf das Zeichen "" am Index 5 folgt.String.Empty, which represents the empty string that follows the "" character at index 5.
Das Trennzeichen ArrayThe separator array
Wenn der- separator
Parameter ist null
oder keine Zeichen enthält, wird davon ausgegangen, dass Leerzeichen als Trennzeichen verwendet werden.If the separator
parameter is null
or contains no characters, white-space characters are assumed to be the delimiters. Leerzeichen werden durch den Unicode-Standard definiert, und die Char.IsWhiteSpace Methode gibt zurück true
, wenn Sie an Sie übermittelt werden.White-space characters are defined by the Unicode standard and the Char.IsWhiteSpace method returns true
if they are passed to it.
Um null
für den- char[] separator
Parameter zu übergeben, müssen Sie den Typ der angeben, null
um den-Befehl von einigen anderen über Ladungen, z. b., zu unterscheiden Split(String[], StringSplitOptions) .To pass null
for the char[] separator
parameter, you must indicate the type of the null
to disambiguate the call from some other overloads, such as Split(String[], StringSplitOptions). Im folgenden Beispiel werden mehrere Möglichkeiten zum eindeutigen Identifizieren dieser Überladung veranschaulicht.The following example shows several ways to unambiguously identify this overload.
string phrase = "The quick brown fox";
_ = phrase.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
_ = phrase.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
_ = phrase.Split(null as char[], StringSplitOptions.RemoveEmptyEntries);
Dim phrase As String = "The quick brown fox"
Dim words() As String
words = phrase.Split(TryCast(Nothing, Char()),
StringSplitOptions.RemoveEmptyEntries)
words = phrase.Split(New Char() {},
StringSplitOptions.RemoveEmptyEntries)
Vergleichs DetailsComparison details
Die Split -Methode extrahiert die Teil Zeichenfolgen in dieser Zeichenfolge, die durch ein oder mehrere der Zeichen des- separator
Parameters getrennt sind, und gibt diese Teil Zeichenfolgen als Elemente eines Arrays zurück.The Split method extracts the substrings in this string that are delimited by one or more of the characters in the separator
parameter, and returns those substrings as elements of an array.
Die- Split Methode sucht nach Trennzeichen, indem Sie Vergleiche mithilfe von ordinalsortierungs Regeln für die Groß-und Kleinschreibung durchführtThe Split method looks for delimiters by performing comparisons using case-sensitive ordinal sort rules. Weitere Informationen zu Wort-, Zeichen folgen-und ordinalsortierungen finden Sie unter der- System.Globalization.CompareOptions Enumeration.For more information about word, string, and ordinal sorts, see the System.Globalization.CompareOptions enumeration.
Überlegungen zur LeistungPerformance considerations
Die Split -Methoden weisen Speicher für das zurückgegebene Array Objekt und ein- String Objekt für jedes Array Element zu.The Split methods allocate memory for the returned array object and a String object for each array element. Wenn Ihre Anwendung eine optimale Leistung erfordert oder wenn die Verwaltung der Speicher Belegung in der Anwendung wichtig ist, sollten Sie die IndexOf IndexOfAny -Methode oder die-Methode und optional die- Compare Methode verwenden, um eine Teil Zeichenfolge innerhalb einer Zeichenfolge zu suchen.If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method, and optionally the Compare method, to locate a substring within a string.
Wenn Sie eine Zeichenfolge an einem Trennzeichen aufteilen, verwenden Sie die- IndexOf Methode oder die- IndexOfAny Methode, um ein Trennzeichen in der Zeichenfolge zu suchen.If you are splitting a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. Wenn Sie eine Zeichenfolge in eine Trenn Zeichenfolge aufteilen, verwenden Sie die- IndexOf oder- IndexOfAny Methode, um das erste Zeichen der Trenn Zeichenfolge zu suchen.If you are splitting a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Verwenden Sie dann die- Compare Methode, um zu bestimmen, ob die Zeichen nach dem ersten Zeichen gleich den verbleibenden Zeichen der Trenn Zeichenfolge sind.Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
Wenn außerdem die gleichen Zeichen folgen zum Aufteilen von Zeichen folgen in mehreren Split Methoden aufrufen verwendet werden, sollten Sie in Erwägung gezogen werden, ein einzelnes Array zu erstellen und in jedem Methodenaufruf darauf zu verweisen.In addition, if the same set of characters is used to split strings in multiple Split method calls, consider creating a single array and referencing it in each method call. Dadurch wird der zusätzliche mehr Aufwand für die einzelnen Methodenaufrufe erheblich reduziert.This significantly reduces the additional overhead of each method call.
Hinweise für Aufrufer
In .NET Framework 3.5.NET Framework 3.5 und früheren Versionen Split(Char[]) separator
verwendet die-Methode null
einen etwas anderen Satz von Leerzeichen, um die Zeichenfolge zu teilen, wenn der Methode ein-Wert mit dem Wert oder keine Zeichen enthält Trim(Char[]) .In .NET Framework 3.5.NET Framework 3.5 and earlier versions, if the Split(Char[]) method is passed a separator
that is null
or contains no characters, the method uses a slightly different set of white-space characters to split the string than the Trim(Char[]) method does to trim the string. Beginnend mit .NET Framework 4 verwenden beide Methoden einen identischen Satz von Unicode-Leerzeichen.Starting with .NET Framework 4, both methods use an identical set of Unicode white-space characters.
Gilt für:
Split(Char[], Int32)
Hier wird eine Zeichenfolge anhand der angegebenen Trennzeichen in eine maximale Anzahl von Teilzeichenfolgen unterteilt.Splits a string into a maximum number of substrings based on specified delimiting characters.
public:
cli::array <System::String ^> ^ Split(cli::array <char> ^ separator, int count);
public string[] Split (char[] separator, int count);
public string[] Split (char[]? separator, int count);
member this.Split : char[] * int -> string[]
Public Function Split (separator As Char(), count As Integer) As String()
Parameter
- separator
- Char[]
Hierbei handelt es sich um ein Array von Zeichen, die die Teilzeichenfolgen dieser Zeichenfolge trennen, ein leeres Array ohne Trennzeichen oder null
.An array of characters that delimit the substrings in this string, an empty array that contains no delimiters, or null
.
- count
- Int32
Die maximale Anzahl der zurückzugebenden Teilzeichenfolgen.The maximum number of substrings to return.
Gibt zurück
- String[]
Ein Array, dessen Elemente die Teilzeichenfolgen in dieser Instanz enthält, die durch ein oder mehr Zeichen aus separator
getrennt sind.An array whose elements contain the substrings in this instance that are delimited by one or more characters in separator
. Weitere Informationen finden Sie im Abschnitt "Hinweise".For more information, see the Remarks section.
Ausnahmen
count
ist ein negativer Wert.count
is negative.
Beispiele
Im folgenden Beispiel wird veranschaulicht count
, wie verwendet werden kann, um die Anzahl der von zurückgegebenen Zeichen folgen einzuschränken Split .The following example demonstrates how count
can be used to limit the number of strings returned by Split.
string name = "Alex Johnson III";
string[] subs = name.Split(null, 2);
string firstName = subs[0];
string lastName;
if (subs.Length > 1)
{
lastName = subs[1];
}
// firstName = "Alex"
// lastName = "Johnson III"
Console.WriteLine("What is your name?")
Dim name As String = Console.ReadLine()
Dim substrings = name.Split(Nothing, 2)
Dim firstName As String = substrings(0)
Dim lastName As String
If substrings.Length > 1 Then
lastName = substrings(1)
End If
' If the user enters "Alex Johnson III":
' firstName = "Alex"
' lastName = "Johnson III"
Hinweise
Trennzeichen sind nicht in den Elementen des zurückgegebenen Arrays enthalten.Delimiter characters are not included in the elements of the returned array.
Wenn diese Instanz keines der Zeichen in enthält separator
, besteht das zurückgegebene Array aus einem einzelnen Element, das diese Instanz enthält.If this instance does not contain any of the characters in separator
, the returned array consists of a single element that contains this instance. Wenn count
0 (null) ist, wird ein leeres Array zurückgegeben.If count
is zero, an empty array is returned.
Wenn der- separator
Parameter ist null
oder keine Zeichen enthält, wird davon ausgegangen, dass Leerzeichen als Trennzeichen verwendet werden.If the separator
parameter is null
or contains no characters, white-space characters are assumed to be the delimiters. Leerzeichen werden durch den Unicode-Standard definiert, und die Char.IsWhiteSpace Methode gibt zurück true
, wenn Sie an Sie übermittelt werden.White-space characters are defined by the Unicode standard and the Char.IsWhiteSpace method returns true
if they are passed to it.
Jedes Element von separator
definiert ein separates Trennzeichen.Each element of separator
defines a separate delimiter character. Wenn zwei Trennzeichen nebeneinander liegen oder ein Trennzeichen am Anfang oder Ende dieser Instanz gefunden wird, enthält das entsprechende Array Element Empty .If two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty.
Wenn in dieser Instanz mehr als Teil Zeichenfolgen vorhanden sind count
, werden die ersten Teil Zeichenfolgen count - 1
in den ersten count - 1
Elementen des Rückgabewerts zurückgegeben, und die restlichen Zeichen in dieser Instanz werden im letzten Element des Rückgabewerts zurückgegeben.If there are more than count
substrings in this instance, the first count - 1
substrings are returned in the first count - 1
elements of the return value, and the remaining characters in this instance are returned in the last element of the return value.
Wenn count
größer als die Anzahl der Teil Zeichenfolgen ist, werden die verfügbaren Teil Zeichenfolgen zurückgegeben, und es wird keine Ausnahme ausgelöst.If count
is greater than the number of substrings, the available substrings are returned and no exception is thrown.
In der folgenden Tabelle sind einige Beispiele aufgeführt.The following table shows some examples.
SpracheLanguage | ZeichenfolgenwertString value | TrennzeichenSeparator | Zurück gegebenes ArrayReturned array |
---|---|---|---|
C#C# | "42, 12, 19""42, 12, 19" | New Char [] {', ', ' '}new Char[] {',', ' '} | {"42", "", "12", "", "19"}{"42", "", "12", "", "19"} |
Visual BasicVisual Basic | "42, 12, 19""42, 12, 19" | Char () = {"," c "," c} "Char() = {","c, " "c}) | {"42", "", "12", "", "19"}{"42", "", "12", "", "19"} |
C#C# | "42.. 12.. 19""42..12..19." | New Char [] {'. '}new Char[] {'.'} | {"42", "", "12", "", "19", ""}{"42", "", "12", "", "19", ""} |
Visual BasicVisual Basic | "42.. 12.. 19""42..12..19." | Char () = {"." scherChar() = {"."c} | {"42", "", "12", "", "19", ""}{"42", "", "12", "", "19", ""} |
C#C# | Bananen"Banana" | New Char [] {'. '}new Char[] {'.'} | {"Banane"}{"Banana"} |
Visual BasicVisual Basic | Bananen"Banana" | Char () = {"." scherChar() = {"."c} | {"Banane"}{"Banana"} |
C#C# | "Darb\nsmarba""Darb\nSmarba" | New Char [] {}new Char[] {} | {"Darb", "Smarba"}{"Darb", "Smarba"} |
Visual BasicVisual Basic | "Darb" & vbLf & "Smarba""Darb" & vbLf & "Smarba" | Char () = {}Char() = {} | {"Darb", "Smarba"}{"Darb", "Smarba"} |
C#C# | "Darb\nsmarba""Darb\nSmarba" | NULLnull | {"Darb", "Smarba"}{"Darb", "Smarba"} |
Visual BasicVisual Basic | "Darb" & vbLf & "Smarba""Darb" & vbLf & "Smarba" | NichtsNothing | {"Darb", "Smarba"}{"Darb", "Smarba"} |
Überlegungen zur LeistungPerformance considerations
Die Split -Methoden weisen Speicher für das zurückgegebene Array Objekt und ein- String Objekt für jedes Array Element zu.The Split methods allocate memory for the returned array object and a String object for each array element. Wenn Ihre Anwendung eine optimale Leistung erfordert oder wenn die Verwaltung der Speicher Belegung in der Anwendung wichtig ist, sollten Sie die IndexOf IndexOfAny -Methode oder die-Methode und optional die- Compare Methode verwenden, um eine Teil Zeichenfolge innerhalb einer Zeichenfolge zu suchen.If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method, and optionally the Compare method, to locate a substring within a string.
Wenn Sie eine Zeichenfolge an einem Trennzeichen aufteilen, verwenden Sie die- IndexOf Methode oder die- IndexOfAny Methode, um ein Trennzeichen in der Zeichenfolge zu suchen.If you are splitting a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. Wenn Sie eine Zeichenfolge in eine Trenn Zeichenfolge aufteilen, verwenden Sie die- IndexOf oder- IndexOfAny Methode, um das erste Zeichen der Trenn Zeichenfolge zu suchen.If you are splitting a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Verwenden Sie dann die- Compare Methode, um zu bestimmen, ob die Zeichen nach dem ersten Zeichen gleich den verbleibenden Zeichen der Trenn Zeichenfolge sind.Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
Wenn außerdem die gleichen Zeichen folgen zum Aufteilen von Zeichen folgen in mehreren Split Methoden aufrufen verwendet werden, sollten Sie in Erwägung gezogen werden, ein einzelnes Array zu erstellen und in jedem Methodenaufruf darauf zu verweisen.In addition, if the same set of characters is used to split strings in multiple Split method calls, consider creating a single array and referencing it in each method call. Dadurch wird der zusätzliche mehr Aufwand für die einzelnen Methodenaufrufe erheblich reduziert.This significantly reduces the additional overhead of each method call.
Hinweise für Aufrufer
In .NET Framework 3.5.NET Framework 3.5 und früheren Versionen Split(Char[]) separator
verwendet die-Methode null
einen etwas anderen Satz von Leerzeichen, um die Zeichenfolge zu teilen, wenn der Methode ein-Wert mit dem Wert oder keine Zeichen enthält Trim(Char[]) .In .NET Framework 3.5.NET Framework 3.5 and earlier versions, if the Split(Char[]) method is passed a separator
that is null
or contains no characters, the method uses a slightly different set of white-space characters to split the string than the Trim(Char[]) method does to trim the string. Beginnend mit .NET Framework 4 verwenden beide Methoden einen identischen Satz von Unicode-Leerzeichen.Starting with .NET Framework 4, both methods use an identical set of Unicode white-space characters.
Weitere Informationen
- Char
- Array
- Int32
- Concat(Object)
- Insert(Int32, String)
- Join(String, String[])
- Remove(Int32, Int32)
- Replace(Char, Char)
- Substring(Int32)
- Trim(Char[])
Gilt für:
Split(Char, StringSplitOptions)
Hier wird eine Zeichenfolge anhand eines angegebenen Trennzeichens und optional von Optionen in Teilzeichenfolgen unterteilt.Splits a string into substrings based on a specified delimiting character and, optionally, options.
public string[] Split (char separator, StringSplitOptions options = System.StringSplitOptions.None);
member this.Split : char * StringSplitOptions -> string[]
Public Function Split (separator As Char, Optional options As StringSplitOptions = System.StringSplitOptions.None) As String()
Parameter
- separator
- Char
Ein Zeichen, das die Teilzeichenfolgen in dieser Zeichenfolge trennt.A character that delimits the substrings in this string.
- options
- StringSplitOptions
Dies ist eine bitweise Kombination der Enumerationswerte, die angibt, ob Teilzeichenfolgen zugeschnitten und leere Teilzeichenfolgen eingeschlossen werden sollen.A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.
Gibt zurück
- String[]
Ein Array, dessen Elemente die Teilzeichenfolgen von dieser Instanz enthält, die durch separator
getrennt sind.An array whose elements contain the substrings from this instance that are delimited by separator
.
Gilt für:
Split(String, StringSplitOptions)
Unterteilt eine Zeichenfolge anhand der angegebenen Zeichenfolgentrennlinie in Teilzeichenfolgen.Splits a string into substrings that are based on the provided string separator.
public string[] Split (string? separator, StringSplitOptions options = System.StringSplitOptions.None);
public string[] Split (string separator, StringSplitOptions options = System.StringSplitOptions.None);
member this.Split : string * StringSplitOptions -> string[]
Public Function Split (separator As String, Optional options As StringSplitOptions = System.StringSplitOptions.None) As String()
Parameter
- separator
- String
Eine Zeichenfolge, die die Teilzeichenfolgen in dieser Zeichenfolge trennt.A string that delimits the substrings in this string.
- options
- StringSplitOptions
Dies ist eine bitweise Kombination der Enumerationswerte, die angibt, ob Teilzeichenfolgen zugeschnitten und leere Teilzeichenfolgen eingeschlossen werden sollen.A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.
Gibt zurück
- String[]
Ein Array, dessen Elemente die Teilzeichenfolgen von dieser Instanz enthält, die durch separator
getrennt sind.An array whose elements contain the substrings from this instance that are delimited by separator
.
Gilt für:
Split(Char[])
Hier wird eine Zeichenfolge anhand der angegebenen Trennzeichen in Teilzeichenfolgen unterteilt.Splits a string into substrings based on specified delimiting characters.
public:
cli::array <System::String ^> ^ Split(... cli::array <char> ^ separator);
public string[] Split (params char[] separator);
public string[] Split (params char[]? separator);
member this.Split : char[] -> string[]
Public Function Split (ParamArray separator As Char()) As String()
Parameter
- separator
- Char[]
Ein Array von Trennzeichen, ein leeres Array, das keine Trennzeichen enthält, oder null
.An array of delimiting characters, an empty array that contains no delimiters, or null
.
Gibt zurück
- String[]
Ein Array, dessen Elemente die Teilzeichenfolgen von dieser Instanz enthält, die durch ein oder mehr Zeichen aus separator
getrennt sind.An array whose elements contain the substrings from this instance that are delimited by one or more characters in separator
. Weitere Informationen finden Sie im Abschnitt "Hinweise".For more information, see the Remarks section.
Beispiele
Im folgenden Beispiel wird veranschaulicht, wie einzelne Wörter aus einem Textblock extrahiert werden, indem das Leerzeichen (
) und das Tabstopp Zeichen ( \t
) als Trennzeichen behandelt werden.The following example demonstrates how to extract individual words from a block of text by treating the space character (
) and tab character (\t
) as delimiters. Die Zeichenfolge, die aufgeteilt wird, enthält beide Zeichen.The string being split includes both of these characters.
string s = "Today\tI'm going to school";
string[] subs = s.Split(' ', '\t');
foreach (var sub in subs)
{
Console.WriteLine($"Substring: {sub}");
}
// This example produces the following output:
//
// Substring: Today
// Substring: I'm
// Substring: going
// Substring: to
// Substring: school
Dim s As String = "Today" & vbTab & "I'm going to school"
Dim subs As String() = s.Split(" "c, Char.Parse(vbTab))
For Each substring In subs
Console.WriteLine("Substring: " & substring)
Next
' This example produces the following output:
'
' Substring: Today
' Substring: I 'm
' Substring: going
' Substring: to
' Substring: school
Hinweise
Wenn eine Zeichenfolge durch einen bekannten Zeichensatz getrennt ist, können Sie die-Methode verwenden, Split(Char[]) um Sie in Teil Zeichenfolgen zu trennen.When a string is delimited by a known set of characters, you can use the Split(Char[]) method to separate it into substrings.
Trennzeichen sind nicht in den Elementen des zurückgegebenen Arrays enthalten.Delimiter characters are not included in the elements of the returned array. Wenn das Trennzeichen Array z. b. das Zeichen "-" enthält und der Wert der aktuellen Zeichen folgen Instanz "AA-BB-CC" ist, gibt die Methode ein Array zurück, das drei Elemente enthält: "AA", "BB" und "CC".For example, if the separator array includes the character "-" and the value of the current string instance is "aa-bb-cc", the method returns an array that contains three elements: "aa", "bb", and "cc".
Wenn diese Instanz keines der Zeichen in enthält separator
, besteht das zurückgegebene Array aus einem einzelnen Element, das diese Instanz enthält.If this instance does not contain any of the characters in separator
, the returned array consists of a single element that contains this instance.
Jedes Element von separator
definiert ein separates Trennzeichen.Each element of separator
defines a separate delimiter character. Wenn zwei Trennzeichen nebeneinander liegen oder ein Trennzeichen am Anfang oder Ende dieser Instanz gefunden wird, enthält das entsprechende Element im zurückgegebenen Array Empty .If two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance, the corresponding element in the returned array contains Empty.
In der folgenden Tabelle sind einige Beispiele aufgeführt.The following table shows some examples.
SpracheLanguage | ZeichenfolgenwertString value | TrennzeichenSeparator | Zurück gegebenes ArrayReturned array |
---|---|---|---|
C#C# | "42, 12, 19""42, 12, 19" | New Char [] {', ', ' '}new Char[] {',', ' '} | {"42", "", "12", "", "19"}{"42", "", "12", "", "19"} |
Visual BasicVisual Basic | "42, 12, 19""42, 12, 19" | Char () = {"," c "," c} "Char() = {","c, " "c}) | {"42", "", "12", "", "19"}{"42", "", "12", "", "19"} |
C#C# | "42.. 12.. 19""42..12..19." | New Char [] {'. '}new Char[] {'.'} | {"42", "", "12", "", "19", ""}{"42", "", "12", "", "19", ""} |
Visual BasicVisual Basic | "42.. 12.. 19""42..12..19." | Char () = {"." scherChar() = {"."c} | {"42", "", "12", "", "19", ""}{"42", "", "12", "", "19", ""} |
C#C# | Bananen"Banana" | New Char [] {'. '}new Char[] {'.'} | {"Banane"}{"Banana"} |
Visual BasicVisual Basic | Bananen"Banana" | Char () = {"." scherChar() = {"."c} | {"Banane"}{"Banana"} |
C#C# | "Darb\nsmarba""Darb\nSmarba" | New Char [] {}new Char[] {} | {"Darb", "Smarba"}{"Darb", "Smarba"} |
Visual BasicVisual Basic | "Darb" & vbLf & "Smarba""Darb" & vbLf & "Smarba" | Char () = {}Char() = {} | {"Darb", "Smarba"}{"Darb", "Smarba"} |
C#C# | "Darb\nsmarba""Darb\nSmarba" | NULLnull | {"Darb", "Smarba"}{"Darb", "Smarba"} |
Visual BasicVisual Basic | "Darb" & vbLf & "Smarba""Darb" & vbLf & "Smarba" | NichtsNothing | {"Darb", "Smarba"}{"Darb", "Smarba"} |
Das Trennzeichen ArrayThe separator array
Jedes Element des Trenn Zeichens definiert ein separates Trennzeichen, das aus einem einzelnen Zeichen besteht.Each element of separator defines a separate delimiter that consists of a single character.
Wenn das- separator
Argument ist null
oder keine Zeichen enthält, behandelt die Methode Leerzeichen als Trennzeichen.If the separator
argument is null
or contains no characters, the method treats white-space characters as the delimiters. Leerzeichen werden durch den Unicode-Standard definiert, und die Char.IsWhiteSpace Methode gibt zurück, true
Wenn ein Leerzeichen an Sie übermittelt wird.White-space characters are defined by the Unicode standard, and the Char.IsWhiteSpace method returns true
if a white-space character is passed to it.
String. Split (Char []) und compilerüberladungs AuflösungString.Split(Char[]) and compiler overload resolution
Obwohl der einzige Parameter für diese Überladung von String.Split ein Zeichen Array ist, können Sie ihn mit einem einzelnen Zeichen aufrufen, wie im folgenden Beispiel gezeigt.Although the single parameter for this overload of String.Split is a character array, you can call it with a single character, as the following example shows.
string value = "This is a short string.";
char delimiter = 's';
string[] substrings = value.Split(delimiter);
foreach (var substring in substrings)
Console.WriteLine(substring);
// The example displays the following output:
// Thi
// i
// a
// hort
// tring.
Dim value As String = "This is a short string."
Dim delimiter As Char = "s"c
Dim substrings() As String = value.Split(delimiter)
For Each substring In substrings
Console.WriteLine(substring)
Next
End Sub
' The example displays the following output:
'
' Thi
' i
' a
' hort
' tring.
Da der- separator
Parameter mit dem- ParamArrayAttribute Attribut versehen ist, interpretieren Compiler ein einzelnes Zeichen als ein Zeichen Array mit einem einzelnen Element.Because the separator
parameter is decorated with the ParamArrayAttribute attribute, compilers will interpret a single character as a single-element character array. Dies ist nicht der Fall für andere String.Split über Ladungen, die einen- separator
Parameter enthalten. Sie müssen diese Überladungen explizit als-Argument übergeben separator
.This is not the case for other String.Split overloads that include a separator
parameter; you must explicitly pass these overloads a character array as the separator
argument.
Vergleichs DetailsComparison details
Die Split(Char[]) -Methode extrahiert die Teil Zeichenfolgen in dieser Zeichenfolge, die durch ein oder mehrere der Zeichen im separator
Array getrennt sind, und gibt diese Teil Zeichenfolgen als Elemente eines Arrays zurück.The Split(Char[]) method extracts the substrings in this string that are delimited by one or more of the characters in the separator
array, and returns those substrings as elements of an array.
Die- Split(Char[]) Methode sucht nach Trennzeichen, indem Sie Vergleiche mithilfe von ordinalsortierungs Regeln für die Groß-und Kleinschreibung durchführtThe Split(Char[]) method looks for delimiters by performing comparisons using case-sensitive ordinal sort rules. Weitere Informationen zu Wort-, Zeichen folgen-und ordinalsortierungen finden Sie unter der- System.Globalization.CompareOptions Enumeration.For more information about word, string, and ordinal sorts, see the System.Globalization.CompareOptions enumeration.
Überlegungen zur LeistungPerformance considerations
Die Split -Methoden weisen Speicher für das zurückgegebene Array Objekt und ein- String Objekt für jedes Array Element zu.The Split methods allocate memory for the returned array object and a String object for each array element. Wenn Ihre Anwendung eine optimale Leistung erfordert oder wenn die Verwaltung der Speicher Belegung in der Anwendung wichtig ist, sollten Sie die- IndexOf Methode oder die- IndexOfAny Methode verwenden.If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method. Sie haben auch die Möglichkeit, die- Compare Methode zu verwenden, um eine Teil Zeichenfolge innerhalb einer Zeichenfolge zu suchen.You also have the option of using the Compare method to locate a substring within a string.
Verwenden Sie die-Methode oder die-Methode, um eine Zeichenfolge mit einem Trennzeichen zu teilen IndexOf IndexOfAny .To split a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. Um eine Zeichenfolge in eine Trenn Zeichenfolge aufzuteilen, verwenden Sie die- IndexOf oder- IndexOfAny Methode, um das erste Zeichen der Trenn Zeichenfolge zu suchen.To split a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Verwenden Sie dann die- Compare Methode, um zu bestimmen, ob die Zeichen nach dem ersten Zeichen gleich den verbleibenden Zeichen der Trenn Zeichenfolge sind.Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
Wenn außerdem die gleichen Zeichen folgen zum Aufteilen von Zeichen folgen in mehreren Split Methoden aufrufen verwendet werden, sollten Sie in Erwägung gezogen werden, ein einzelnes Array zu erstellen und in jedem Methodenaufruf darauf zu verweisen.In addition, if the same set of characters is used to split strings in multiple Split method calls, consider creating a single array and referencing it in each method call. Dadurch wird der zusätzliche mehr Aufwand für die einzelnen Methodenaufrufe erheblich reduziert.This significantly reduces the additional overhead of each method call.
Hinweise für Aufrufer
In .NET Framework 3.5.NET Framework 3.5 und früheren Versionen Split(Char[]) separator
verwendet die-Methode null
einen etwas anderen Satz von Leerzeichen, um die Zeichenfolge zu teilen, wenn der Methode ein-Wert mit dem Wert oder keine Zeichen enthält Trim(Char[]) .In .NET Framework 3.5.NET Framework 3.5 and earlier versions, if the Split(Char[]) method is passed a separator
that is null
or contains no characters, the method uses a slightly different set of white-space characters to split the string than the Trim(Char[]) method does to trim the string. Beginnend mit .NET Framework 4 verwenden beide Methoden einen identischen Satz von Unicode-Leerzeichen.Starting with .NET Framework 4, both methods use an identical set of Unicode white-space characters.
Weitere Informationen
- Char
- Concat(Object)
- Insert(Int32, String)
- Join(String, String[])
- Remove(Int32, Int32)
- Replace(Char, Char)
- Substring(Int32)
- Trim(Char[])