共用方式為


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

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

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)

Syntax

'Declaration
<ComVisibleAttribute(False)> _
Public Function Split ( _
    separator As Char(), _
    options As StringSplitOptions _
) As String()
[ComVisibleAttribute(false)]
public string[] Split(
    char[] separator,
    StringSplitOptions options
)

Parameters

  • separator
    Type: array<System.Char[]
    An array of Unicode characters that delimit the substrings in this string, an empty array that contains no delimiters, or nulla 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 characters in separator. For more information, see the Remarks section.

Exceptions

Exception Condition
ArgumentException

options is not a member of the StringSplitOptions enumeration.

Remarks

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 nulla null reference (Nothing in Visual Basic) 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(array<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 nulla null reference (Nothing in Visual Basic), 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.

Dim phrase As String = "The quick brown fox"
Dim words() As String

words = phrase.Split(TryCast(Nothing, Char()),  
                       StringSplitOptions.RemoveEmptyEntries)

words = phrase.Split(New Char() {}, 
                     StringSplitOptions.RemoveEmptyEntries)
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.

Examples

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

Change History

Date

History

Reason

October 2010

Addressed compiler overload resolution in the Remarks section.

Customer feedback.