String.Split Method (array<String[], StringSplitOptions)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. A parameter specifies whether to return empty array elements.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(False)> _
Public Function Split ( _
    separator As String(), _
    options As StringSplitOptions _
) As String()
[ComVisibleAttribute(false)]
public string[] Split(
    string[] separator,
    StringSplitOptions options
)

Parameters

  • separator
    Type: array<System.String[]
    An array of strings that delimits the substrings in this string, an empty array that contains no delimiters, or nulla null reference (Nothing in Visual Basic).
  • options
    Type: System.StringSplitOptions
    A flag that indicates whether to include empty elements in the returned array. Specify RemoveEmptyEntries to omit empty array elements from the returned array. Specify None to include empty array elements in the returned array.

Return Value

Type: array<System.String[]
An array whose elements contain the substrings in this string that are delimited by one or more strings in separator. For more information, see the Remarks section.

Exceptions

Exception Condition
ArgumentException

options is not a member of the StringSplitOptions enumeration.

Remarks

Return Value Details

Delimiter strings are not included in the elements of the returned array.

If this instance does not contain any of the strings in separator, the returned array consists of a single element that contains this instance. If the separator parameter is nulla null reference (Nothing in Visual Basic) or contains no characters, white-space characters are assumed to be the delimiters. For a list of the characters that the Split method interprets as white space, see the table in the Remarks section of the String.Split(array<Char[]) method. (Note that this list is slightly different from the list of white-space characters recognized by the Trim() method.) However, if the separator parameter in the call to this method overload is nulla null reference (Nothing in Visual Basic), compiler overload resolution fails. To unambiguously identify the called method, your code must indicate the type of the nulla null reference (Nothing in Visual Basic). The following example shows several ways to unambiguously identify this overload.

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)
string phrase = "The quick  brown fox";
string[] words;

words = phrase.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);

words = phrase.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);

words = phrase.Split(null as string[], StringSplitOptions.RemoveEmptyEntries);

If the options parameter is RemoveEmptyEntries and the length of this instance is zero, an empty array is returned.

Each element of separator defines a separate delimiter string. 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.

Comparison Details

The Split(array<String[], StringSplitOptions) 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.

The Split method looks for delimiters by performing comparisons using case-sensitive ordinal sort rules. For more information about word, string, and ordinal sorts, see the System.Globalization.CompareOptions enumeration.

The Split(array<String[], StringSplitOptions) method ignores any element of separator whose value is nulla null reference (Nothing in Visual Basic) or the empty string ("").

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. The order in which substrings are encountered in the instance takes precedence over the order of elements in separator.

For example, consider an instance whose value is "abcdef". 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". This is because the substring in the instance, "bcde", is encountered and matches an element in separator before the substring "f" is encountered.

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". This is because "bcd" is the first delimiter in separator that matches a delimiter in the instance. 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".

Performance Considerations

The 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.

Examples

The following example illustrates the difference in the arrays returned by calling a string's String.Split(array<String[], StringSplitOptions) method with its options parameter equal to StringSplitOptions.None and StringSplitOptions.RemoveEmptyEntries.

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.
outputBlock.Text &= String.Format("Splitting the string:{0}   '{1}'.", vbCrLf, source) & vbCrLf
outputBlock.Text &= vbCrLf
outputBlock.Text &= String.Format("Using the delimiter string:{0}   '{1}'.", _
                  vbCrLf, stringSeparators(0)) & vbCrLf
outputBlock.Text &= vbCrLf

' Split a string delimited by another string and return all elements.
result = source.Split(stringSeparators, StringSplitOptions.None)
outputBlock.Text &= String.Format("Result including all elements ({0} elements) & vbCrLf:", _
                  result.Length)
outputBlock.Text &= "   "
For Each s As String In result
   outputBlock.Text &= String.Format("'{0}' ", IIf(String.IsNullOrEmpty(s), "<>", s))
Next
outputBlock.Text &= vbCrLf
outputBlock.Text &= vbCrLf

' Split delimited by another string and return all non-empty elements.
result = source.Split(stringSeparators, _
                      StringSplitOptions.RemoveEmptyEntries)
outputBlock.Text &= String.Format("Result including non-empty elements ({0} elements) & vbCrLf:", _
                  result.Length)
outputBlock.Text &= "   "
For Each s As String In result
   outputBlock.Text &= String.Format("'{0}' ", IIf(String.IsNullOrEmpty(s), "<>", s))
Next
outputBlock.Text &= vbCrLf
' This 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 using StringSplitOptions.None (all elements):
'          '<>' 'ONE' '<>' 'TWO' '<>' '<>' 'THREE' '<>' '<>'
'            
'       Result using StringSplitOptions.RemoveEmptyEntries:
'          'ONE' 'TWO' 'THREE'
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.
outputBlock.Text += String.Format("Splitting the string:\n   \"{0}\".", source) + "\n";
outputBlock.Text += "\n";
outputBlock.Text += String.Format("Using the delimiter string:\n   \"{0}\"",
                  stringSeparators[0]) + "\n";
outputBlock.Text += "\n";

// Split a string delimited by another string and return all elements.
result = source.Split(stringSeparators, StringSplitOptions.None);
outputBlock.Text += String.Format("Result including all elements ({0} elements):",
                  result.Length) + "\n";
outputBlock.Text += "   ";
foreach (string s in result)
{
   outputBlock.Text += String.Format("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
outputBlock.Text += "\n";
outputBlock.Text += "\n";

// Split delimited by another string and return all non-empty elements.
result = source.Split(stringSeparators,
                      StringSplitOptions.RemoveEmptyEntries);
outputBlock.Text += String.Format("Result including non-empty elements ({0} elements):",
                  result.Length) + "\n";
outputBlock.Text += "   ";
foreach (string s in result)
{
   outputBlock.Text += String.Format("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
outputBlock.Text += "\n";
/*
This 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 using StringSplitOptions.None (all elements):
   '<>' 'ONE' '<>' 'TWO' '<>' '<>' 'THREE' '<>' '<>'

Result using StringSplitOptions.RemoveEmptyEntries:
   'ONE' 'TWO' 'THREE'
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

October 2010

Addressed compiler overload resolution in the Remarks section.

Customer feedback.