Export (0) Print
Expand All

String.Split Method (Char[], StringSplitOptions)

Updated: October 2010

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

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

[ComVisibleAttribute(false)]
public string[] Split(
	char[] separator,
	StringSplitOptions options
)

Parameters

separator
Type: System.Char[]
An array of Unicode characters that delimit the substrings in this string, an empty array that contains no delimiters, or null.
options
Type: System.StringSplitOptions
A flag that indicates whether to include empty elements in the returned array. Specify StringSplitOptions.RemoveEmptyEntries to omit empty elements from the returned array. Specify StringSplitOptions.None to include empty elements in the returned array.

Return Value

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

ExceptionCondition
ArgumentException

options is not a member of the StringSplitOptions enumeration.

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. 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(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 null, compiler overload resolution fails. To unambiguously identify the called method, your code must indicate the type of the null. The following example shows several ways to unambiguously identify this overload.


string phrase = "The quick  brown fox";
string[] words;

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

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

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


Each element of separator defines a separate delimiter character. If the options parameter is RemoveEmptyEntries and the length of this instance is zero, an empty array is returned.

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.

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.

The following example uses the StringSplitOptions enumeration to include or exclude substrings returned by the Split method.


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>

*/


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

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

Date

History

Reason

October 2010

Addressed compiler overload resolution in the Remarks section.

Customer feedback.

Community Additions

Show:
© 2014 Microsoft