다음을 통해 공유


StringSplitOptions Enumeration

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

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)

Syntax

'Declaration
<FlagsAttribute> _
<ComVisibleAttribute(False)> _
Public Enumeration StringSplitOptions
[FlagsAttribute]
[ComVisibleAttribute(false)]
public enum StringSplitOptions

Members

Member name Description
Supported by Silverlight for Windows PhoneSupported by Xbox 360 None The return value includes array elements that contain an empty string
Supported by Silverlight for Windows PhoneSupported by Xbox 360 RemoveEmptyEntries The return value does not include array elements that contain an empty string

Remarks

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

Examples

The following code 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

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.

See Also

Reference