System.String.Split Method

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.

Syntax

public string[] Split (params char[] separator)

Parameters

separator
An array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters, or null.

Returns

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 null 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(char) 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 string.Empty. The following table provides examples.

"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 Considerations

The erload:System.String.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 erload:System.String.IndexOf or erload:System.String.IndexOfAny method, and optionally the erload:System.String.Compare method, to locate a substring within a string.

If you are splitting a string at a separator character, use the erload:System.String.IndexOf or erload:System.String.IndexOfAny method to locate a separator character in the string. If you are splitting a string at a separator string, use the erload:System.String.IndexOf or erload:System.String.IndexOfAny method to locate the first character of the separator string. Then use the erload:System.String.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 erload:System.String.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.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0