Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

StringSplitOptions Enumeration

Specifies whether methods that split delimited substrings include or omit empty substrings from the returned array.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[FlagsAttribute]
[ComVisibleAttribute(false)]
public enum StringSplitOptions
Member nameDescription
Supported by Silverlight for Windows PhoneSupported by Xbox 360NoneThe return value includes array elements that contain an empty string
Supported by Silverlight for Windows PhoneSupported by Xbox 360RemoveEmptyEntriesThe return value does not include array elements that contain an empty string

The String.Split(Char[], StringSplitOptions) and String.Split(String[], StringSplitOptions) methods return an array of the substrings in a given string that are delimited by specified characters or strings. Adjacent delimiters yield an array element that contains an empty string (""). The values of the StringSplitOptions enumeration specify whether an array element that contains an empty string is included in the returned array.

Specify the StringSplitOptions.None value to invoke the default behavior of the Split method, which is to return an array of both empty substrings and substrings that are not empty. Specify the StringSplitOptions.RemoveEmptyEntries value to cause the Split method to return an array consisting solely of substrings that are not empty.

The following code example uses the StringSplitOptions enumeration to include or exclude substrings generated 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.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.