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

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.

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

Syntax

Public Function Split ( _
    separator As String(), _
    count As Integer, _
    options As StringSplitOptions _
) As String()
public string[] Split(
    string[] separator,
    int count,
    StringSplitOptions options
)

Parameters

  • separator
    Type: array<System..::.String>[]()[]
    An array of strings that delimit the substrings in this string, an empty array that contains no delimiters, or nullNothingnullptra null reference (Nothing in Visual Basic).

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
ArgumentOutOfRangeException

count is negative.

ArgumentException

options is not one of the StringSplitOptions values.

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, or the count parameter is 1, 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. However, if the separator parameter in the call to this method overload is nullNothingnullptra null reference (Nothing in Visual Basic), compiler overload resolution fails. To unambiguously identify the called method, your code must indicate the type of the nullNothingnullptra null reference (Nothing in Visual Basic). The following example shows several ways to unambiguously identify this overload.

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.

Each element of separator defines a separate delimiter that consists of one or more characters. 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.

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.

If count is greater than the number of substrings, the available substrings are returned and no exception is thrown.

Comparison Details

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.

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 method ignores any element of separator whose value is nullNothingnullptra null reference (Nothing in Visual Basic) or the empty string ("").

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

Notes to Callers

In 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 uses the StringSplitOptions enumeration to include or exclude substrings generated by the Split method.

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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 stringSeparator() As String = {"[stop]"}
      Dim result() As String
      ' ------------------------------------------------------------------------------
      ' Split a string delimited by characters.
      ' ------------------------------------------------------------------------------
      outputBlock.Text &= "1) Split a string delimited by characters:" & vbCrLf & vbCrLf

      ' Display the original string and delimiter characters.
      outputBlock.Text += String.Format("1a )The original string is ""{0}"".", s1) & vbCrLf
      outputBlock.Text += String.Format("The delimiter character is '{0}'." & vbCrLf, charSeparators(0)) & vbCrLf

      ' Split a string delimited by characters and return all elements.
      outputBlock.Text &= "1b) Split a string delimited by characters and " & _
                        "return all elements:"
      result = s1.Split(charSeparators, StringSplitOptions.None)
      Show(outputBlock, result)

      ' Split a string delimited by characters and return all non-empty elements.
      outputBlock.Text &= "1c) Split a string delimited by characters and " & _
                        "return all non-empty elements:"
      result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)
      Show(outputBlock, result)

      ' ------------------------------------------------------------------------------
      ' Split a string delimited by another string.
      ' ------------------------------------------------------------------------------
      outputBlock.Text &= "2) Split a string delimited by another string:" & vbCrLf & vbCrLf

      ' Display the original string and delimiter string.
      outputBlock.Text += String.Format("2a) The original string is ""{0}"".", s2) & vbCrLf
      outputBlock.Text += String.Format("The delimiter string is ""{0}""." & vbCrLf, stringSeparator(0)) & vbCrLf

      ' Split a string delimited by another string and return all elements.
      outputBlock.Text &= "2b) Split a string delimited by another string and " & _
                        "return all elements:"
      result = s2.Split(stringSeparator, StringSplitOptions.None)
      Show(outputBlock, result)

      ' Split the original string at the delimiter and return all non-empty elements.
      outputBlock.Text &= "2c) Split a string delimited by another string and " & _
                        "return all non-empty elements:"
      result = s2.Split(stringSeparator, StringSplitOptions.RemoveEmptyEntries)
      Show(outputBlock, result)
   End Sub 

   ' Display the array of separated strings.
   Public Shared Sub Show(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal entries() As String)
      outputBlock.Text += String.Format("The return value contains these {0} elements:", entries.Length) & vbCrLf
      Dim entry As String
      For Each entry In entries
         outputBlock.Text += String.Format("<{0}>", entry)
      Next entry
      outputBlock.Text &= vbCrLf & vbCrLf

   End Sub 
End Class 
'
'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>
'
'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>
'
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string s1 = ",ONE,,TWO,,,THREE,,";
      string s2 = "[stop]" +
                  "ONE[stop][stop]" +
                  "TWO[stop][stop][stop]" +
                  "THREE[stop][stop]";
      char[] charSeparators = { ',' };
      string[] stringSeparator = {"[stop]"};
      string[] result;
      // ------------------------------------------------------------------------------
      // Split a string delimited by characters.
      // ------------------------------------------------------------------------------
      outputBlock.Text += "1) Split a string delimited by characters:\n" + "\n";

      // Display the original string and delimiter characters.
      outputBlock.Text += String.Format("1a )The original string is \"{0}\".", s1) + "\n";
      outputBlock.Text += String.Format("The delimiter character is '{0}'.\n",
                         charSeparators[0]) + "\n";

      // Split a string delimited by characters and return all elements.
      outputBlock.Text += String.Format("1b) Split a string delimited by characters and " +
                        "return all elements:") + "\n";
      result = s1.Split(charSeparators, StringSplitOptions.None);
      Show(outputBlock, result);

      // Split a string delimited by characters and return all non-empty elements.
      outputBlock.Text += String.Format("1c) Split a string delimited by characters and " +
                        "return all non-empty elements:") + "\n";
      result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
      Show(outputBlock, result);

      // ------------------------------------------------------------------------------
      // Split a string delimited by another string.
      // ------------------------------------------------------------------------------
      outputBlock.Text += "2) Split a string delimited by another string:\n" + "\n";

      // Display the original string and delimiter string.
      outputBlock.Text += String.Format("2a) The original string is \"{0}\".", s2) + "\n";
      outputBlock.Text += String.Format("The delimiter string is \"{0}\".\n", stringSeparator[0]) + "\n";

      // Split a string delimited by another string and return all elements.
      outputBlock.Text += String.Format("2b) Split a string delimited by another string and " +
                        "return all elements:") + "\n";
      result = s2.Split(stringSeparator, StringSplitOptions.None);
      Show(outputBlock, result);

      // Split the original string at the delimiter and return all non-empty elements.
      outputBlock.Text += String.Format("2c) Split a string delimited by another string and " +
                        "return all non-empty elements:") + "\n";
      result = s2.Split(stringSeparator, StringSplitOptions.RemoveEmptyEntries);
      Show(outputBlock, result);
   }

   // Display the array of separated strings.
   public static void Show(System.Windows.Controls.TextBlock outputBlock, string[] entries)
   {
      outputBlock.Text += String.Format("The return value contains these {0} elements:", entries.Length) + "\n";
      foreach (string entry in entries)
      {
         outputBlock.Text += String.Format("<{0}>", entry);
      }
      outputBlock.Text += "\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>

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>

*/

Version Information

Windows Phone OS

Supported in: 8.1, 8.0

See Also

Reference

String Class

Split Overload

System Namespace