Compartir a través de


Regex.Split Method (String, Int32, Int32)

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

Splits the specified input string a specified maximum number of times at the positions defined by a regular expression specified in the Regex constructor. The search for the regular expression pattern starts at a specified character position in the input string.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

Syntax

'Declaration
Public Function Split ( _
    input As String, _
    count As Integer, _
    startat As Integer _
) As String()
public string[] Split(
    string input,
    int count,
    int startat
)

Parameters

  • count
    Type: System.Int32
    The maximum number of times the split can occur.
  • startat
    Type: System.Int32
    The character position in the input string where the search will begin.

Return Value

Type: array<System.String[]
An array of strings.

Exceptions

Exception Condition
ArgumentNullException

input is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

startat is less than zero or greater than the length of input.

Remarks

The Regex.Split methods are similar to the String.Split method, except this method splits the string at a delimiter determined by a regular expression instead of a set of characters. The count parameter specifies the maximum number of substrings into which the input string is split; the last string contains the unsplit remainder of the string. A count value of zero provides the default behavior of splitting as many times as possible. The startat parameter defines the point at which the search for the first delimiter begins (this can be used for skipping leading white space).

If no matches are found from the count+1 position in the string, the method returns a one-element array that contains the input string. If one or more matches are found, the first element of the returned array contains the first portion of the string from the first character up to one character before the match.

If multiple matches are adjacent to one another and the number of matches found is at least two less than count, an empty string is inserted into the array. That is, empty strings that result from adjacent matches are counted in determining whether the number of matched substrings equals count.

If capturing parentheses are used in a regular expression, any captured text is included in the array of split strings. However, any array elements that contain captured text are not counted in determining whether the number of matches has reached count. For example, splitting the string '"apple-apricot-plum-pear-pomegranate-pineapple-peach" into a maximum of four substrings beginning at character 15 in the string results in a seven-element array, as the following code shows.

Dim pattern As String = "(-)"
Dim input As String = "apple-apricot-plum-pear-pomegranate-pineapple-peach"

Dim regex As Regex = New Regex(pattern)
' Split on hyphens from 15th character on
Dim substrings() As String = regex.Split(input, 4, 15)
For Each match As String In substrings
   outputBlock.Text += String.Format("'{0}'", match) & vbCrLf
Next
' The method writes the following:
'    'apple-apricot-plum'
'    '-'
'    'pear'
'    '-'
'    'pomegranate'
'    '-'
'    'pineapple-peach'      
string pattern = "(-)";
string input = "apple-apricot-plum-pear-pomegranate-pineapple-peach";

// Split on hyphens from 15th character on
Regex regex = new Regex(pattern);
// Split on hyphens from 15th character on
string[] substrings = regex.Split(input, 4, 15);
foreach (string match in substrings)
{
   outputBlock.Text += String.Format("'{0}'", match) + "\n";
}
// The method writes the following:
//    'apple-apricot-plum'
//    '-'
//    'pear'
//    '-'
//    'pomegranate'
//    '-'
//    'pineapple-peach'      

When the regular expression pattern includes multiple sets of capturing parentheses, all captured text is added to the returned array. For example, the following code uses two sets of capturing parentheses to extract the individual words in a string. The first set of capturing parentheses captures the hyphen, while the second set captures the vertical bar. The array returned by the method includes the vertical bar characters.

Dim pattern As String = "(-)|([|])"   ' possible delimiters found in string
Dim input As String = "apple|apricot|plum|pear|pomegranate|pineapple|peach"

Dim regex As Regex = New Regex(pattern)
' Split on delimiters from 15th character on
Dim substrings() As String = regex.Split(input, 4, 15)
For Each match As String In substrings
   outputBlock.Text += String.Format("'{0}'", match) & vbCrLf
Next
' The method returns an array of 7 elements, as follows:
'    apple|apricot|plum'
'    '|'
'    'pear'
'    '|'
'    'pomegranate'
'    '|'
'    'pineapple|peach'
string pattern = "(-)|([|])";     // possible delimiters found in string
string input = "apple|apricot|plum|pear|pomegranate|pineapple|peach";

Regex regex = new Regex(pattern);
// Split on delimiters from 15th character on
string[] substrings = regex.Split(input, 4, 15);
foreach (string match in substrings)
{
   outputBlock.Text += String.Format("'{0}'", match) + "\n";
}
// The method returns an array of 7 elements, as follows:
//    apple|apricot|plum'
//    '|'
//    'pear'
//    '|'
//    'pomegranate'
//    '|'
//    'pineapple|peach'

If the regular expression can match the empty string, Split(String, Int32, Int32) will split the string into an array of single-character strings because the empty string delimiter can be found at every location. The following example splits the string "characters" into as many elements as the input string contains, starting with the character "a". Because the null string matches the end of the input string, a null string is inserted at the end of the returned array.

Dim input As String = "characters"
Dim regex As New Regex("")
Dim substrings() As String = regex.Split(input, input.Length, _
                                         input.IndexOf("a"))
outputBlock.Text &= "{"
For ctr As Integer = 0 To substrings.Length - 1
   outputBlock.Text &= substrings(ctr)
   If ctr < substrings.Length - 1 Then outputBlock.Text &= ", "
Next
outputBlock.Text &= "}" & vbCrLf
' The example produces the following output:   
'    {, c, h, a, r, a, c, t, e, rs}
string input = "characters";
Regex regex = new Regex("");
string[] substrings = regex.Split(input, input.Length, input.IndexOf("a"));
outputBlock.Text += "{";
for (int ctr = 0; ctr < substrings.Length; ctr++)
{
   outputBlock.Text += substrings[ctr];
   if (ctr < substrings.Length - 1)
      outputBlock.Text += ", ";
}
outputBlock.Text += "}" + "\n";
// The example produces the following output:   
//    {, c, h, a, r, a, c, t, e, rs}

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.