StringSplitOptions Enumeration
TOC
Collapse the table of content
Expand the table of content

StringSplitOptions Enumeration

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

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)

'Declaration
<FlagsAttribute> _
Public Enumeration StringSplitOptions

Member nameDescription
NoneThe return value includes array elements that contain an empty string
RemoveEmptyEntriesThe 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.


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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft