.NET Framework Class Library String..::.Split Method (array<Char>[]()[]) Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)

Syntax
Public Function Split ( _
ParamArray separator As Char() _
) As String()
public string[] Split(
params char[] separator
)
public:
array<String^>^ Split(
... array<wchar_t>^ separator
)
member Split :
separator:char[] -> string[]
Parameters- separator
- Type: array<System..::.Char>[]()[]
An array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters, or nullNothingnullptra null reference (Nothing in Visual Basic).
Return ValueType: array<System..::.String>[]()[]An array whose elements contain the substrings in this instance that are delimited by one or more characters in separator. For more information, see the Remarks section.

Remarks
Delimiter characters are not included in the elements of the returned array. If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance. If the separator parameter is nullNothingnullptra null reference (Nothing in Visual Basic) or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the Char..::.IsWhiteSpace method. Each element of separator defines a separate delimiter character. If two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty. The following table provides examples. String value | Separator | Returned array |
|---|
"42, 12, 19" | new Char[] {',', ' '} (C#) Char() = {","c, " "c}) (Visual Basic) | {"42", "", "12", "", "19"} | "42..12..19" | new Char[] {'.'} (C#) Char() = {"."c} (Visual Basic) | {"42", "", "12", "", "19"} | "Banana" | new Char[] {'.'} (C#) Char() = {"."c} (Visual Basic) | {"Banana"} | "Darb\nSmarba" (C#) "Darb" & vbLf & "Smarba" (Visual Basic) | new Char[] {} (C#) Char() = {} (Visual Basic) | {"Darb", "Smarba"} | "Darb\nSmarba" (C#) "Darb" & vbLf & "Smarba" (Visual Basic) | null (C#) Nothing (Visual Basic) | {"Darb", "Smarba"} |
Performance ConsiderationsThe Split methods allocate memory for the returned array object and a String object for each array element. 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. If you are splitting a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. 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. Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string. 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. This significantly reduces the additional overhead of each method call. Notes to CallersIn the .NET Framework 3.5 and earlier versions, if the Split method is passed a separator that is nullNothingnullptra null reference (Nothing in Visual Basic) or contains no characters, the method uses a slightly different set of characters to split the string than the Trim method does to trim the string. In the .NET Framework 4, both methods use an identical set of Unicode white-space characters.

Examples
The following example demonstrates how to extract individual words from a block of text by treating white space and punctuation marks as delimiters. The character array passed to the separator parameter of the String..::.Split(array<Char>[]()[]) method consists of a space character and a tab character, together with some common punctuation symbols.
Public Class SplitTest
Public Shared Sub Main()
Dim words As String = "This is a list of words, with: a bit of punctuation" + _
vbTab + "and a tab character."
Dim split As String() = words.Split(New [Char]() {" "c, ","c, "."c, ":"c, CChar(vbTab) })
For Each s As String In split
If s.Trim() <> "" Then
Console.WriteLine(s)
End If
Next s
End Sub 'Main
End Class 'SplitTest
' The example displays the following output to the console:
' This
' is
' a
' list
' of
' words
' with
' a
' bit
' of
' punctuation
' and
' a
' tab
' character
using System;
public class SplitTest {
public static void Main() {
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
foreach (string s in split) {
if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character

Version Information
.NET FrameworkSupported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileSupported in: 4, 3.5 SP1 Portable Class LibrarySupported in: Portable Class Library

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

See Also
|
.NET Framework-Klassenbibliothek String..::.Split-Methode (array<Char>[]()[]) Gibt ein Zeichenfolgenarray zurück, das die Teilzeichenfolgen dieser Instanz enthält, die durch Elemente eines angegebenen Unicode-Zeichenarrays getrennt sind.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)

Syntax
Public Function Split ( _
ParamArray separator As Char() _
) As String()
public string[] Split(
params char[] separator
)
public:
array<String^>^ Split(
... array<wchar_t>^ separator
)
member Split :
separator:char[] -> string[]
Parameter- separator
- Typ: array<System..::.Char>[]()[]
Ein Array von Unicode-Zeichen, die die Teilzeichenfolgen dieser Instanz trennen, ein leeres Array ohne Trennzeichen oder nullNothingnullptrNULL-Verweis (Nothing in Visual Basic).
RückgabewertTyp: array<System..::.String>[]()[]Ein Array, dessen Elemente die Teilzeichenfolgen in dieser Instanz enthält, die durch ein oder mehr Zeichen aus separator getrennt sind. Weitere Informationen finden Sie im Abschnitt Hinweise.

Hinweise
Trennzeichen sind in den Elementen des zurückgegebenen Arrays nicht enthalten. Wenn diese Instanz keine der Zeichen in separator enthält, besteht das zurückgegebene Array aus einem einzelnen Element, das diese Instanz enthält. Wenn der separator-Parameter nullNothingnullptrNULL-Verweis (Nothing in Visual Basic) ist oder keine Zeichen enthält, werden Leerraumzeichen als Trennzeichen angenommen. Leerstellenzeichen werden vom Unicodestandard definiert und geben true zurück, wenn sie an die Char..::.IsWhiteSpace-Methode übergeben werden. Jedes Element von separator definiert ein separates Trennzeichen. Wenn zwei Trennzeichen aufeinander folgen oder ein Trennzeichen sich am Anfang oder Ende dieser Instanz befinden, enthält das entsprechende Arrayelement Empty. Der folgende Tabelle enthält Beispiele. Zeichenfolgenwert | Trennzeichen | Zurückgegebenes Array |
|---|
"42, 12, 19" | new Char[] {',', ' '} (C#) Char() = {","c, " "c} (Visual Basic) | {"42", "", "12", "", "19"} | "42..12..19" | new Char[] {'.'} (C#) Char() = {"."c} (Visual Basic) | {"42", "", "12", "", "19"} | "Banana" | new Char[] {'.'} (C#) Char() = {"."c} (Visual Basic) | {"Banana"} | "Darb\nSmarba" (C#) "Darb" & vbLf & "Smarba" (Visual Basic) | new Char[] {} (C#) Char() = {} (Visual Basic) | {"Darb", "Smarba"} | "Darb\nSmarba" (C#) "Darb" & vbLf & "Smarba" (Visual Basic) | null (C#) Nothing (Visual Basic) | {"Darb", "Smarba"} |
Überlegungen zur LeistungDie Split-Methoden belegen Speicher für das zurückgegebene Arrayobjekt und ein String-Objekt für jedes Arrayelement. Wenn die optimale Leistung oder die Verwaltung der Speicherbelegung für die Anwendung eine wichtige Rolle spielt, empfiehlt es sich, zum Suchen einer Teilzeichenfolge in einer Zeichenfolge die IndexOf-Methode oder die IndexOfAny-Methode sowie optional die Compare-Methode zu verwenden. Wenn Sie eine Zeichenfolge an einem Trennzeichen teilen, verwenden Sie zum Suchen eines Trennzeichens in der Zeichenfolge die IndexOf-Methode oder die IndexOfAny-Methode. Wenn Sie eine Zeichenfolge an einem Trennzeichen teilen, verwenden Sie zum Suchen des ersten Zeichens einer Trennzeichenfolge die IndexOf-Methode oder die IndexOfAny-Methode. Bestimmen Sie anschließend mit der Compare-Methode, ob die Zeichen hinter dem ersten Zeichen gleich den verbleibenden Zeichen in der Trennzeichenfolge sind. Wenn außerdem Zeichenfolgen in mehreren Split-Methodenaufrufen mit derselben Gruppe von Zeichen geteilt werden, sollten Sie ein einzelnes Array erstellen und in jedem Methodenaufruf darauf verweisen. Dies reduziert den zusätzlichen Aufwand jedes Methodenaufrufs bedeutend. Hinweise zu AufrufernIn .NET Framework 3.5 und früheren Versionen: Wenn der Split-Methode ein separator, der nullNothingnullptrNULL-Verweis (Nothing in Visual Basic) ist oder keine Zeichen enthält, übergeben wird, verwendet die Methode einen etwas anderen Satz von Zeichen, um die Zeichenfolge zu teilen, als die Trim-Methode. In .NET Framework 4 verwenden beide Methoden einen identischen Satz von Unicode-Leerstellenzeichen.

Beispiele
Im folgenden Beispiel wird veranschaulicht, wie einzelne Wörter aus einem Textblock extrahiert werden, indem Leerraum und Interpunktionszeichen als Trennzeichen behandelt werden. Das an den separator-Parameter der String..::.Split(array<Char>[]()[])-Methode übergebene Zeichenarray besteht aus einem Leerzeichen und einem Tabstoppzeichen zusammen mit einigen häufig verwendeten Interpunktionszeichen.
Public Class SplitTest
Public Shared Sub Main()
Dim words As String = "This is a list of words, with: a bit of punctuation" + _
vbTab + "and a tab character."
Dim split As String() = words.Split(New [Char]() {" "c, ","c, "."c, ":"c, CChar(vbTab) })
For Each s As String In split
If s.Trim() <> "" Then
Console.WriteLine(s)
End If
Next s
End Sub 'Main
End Class 'SplitTest
' The example displays the following output to the console:
' This
' is
' a
' list
' of
' words
' with
' a
' bit
' of
' punctuation
' and
' a
' tab
' character
using System;
public class SplitTest {
public static void Main() {
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
foreach (string s in split) {
if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character

Versionsinformationen
.NET FrameworkUnterstützt in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileUnterstützt in: 4, 3.5 SP1 Unterstützt in:

Plattformen
Windows 7, Windows Vista SP1 oder höher, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core wird nicht unterstützt), Windows Server 2008 R2 (Server Core wird mit SP1 oder höher unterstützt), Windows Server 2003 SP2
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

Siehe auch
|