Regex.Split Method (String, Int32)

Splits the specified input string a specified maximum number of times at the positions defined by a regular expression specified in the Regex constructor.

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

public string[] Split(
	string input,
	int count


Type: System.String
The string to be split.
Type: System.Int32
The maximum number of times the split can occur.

Return Value

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


input is null.

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 can be 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.

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-banana" into a maximum of four substrings results in a seven-element array, as the following code shows.

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

When the regular expression pattern includes multiple sets of capturing parentheses, all captured text is added to the returned array. However, elements in the returned array that contain captured text are not counted in determining whether the number of matched substrings equals count. For example, in the following code, a regular expression uses two sets of capturing parentheses to extract the elements of a date from a date string. The first set of capturing parentheses captures the hyphen, while the second set captures the forward slash. The call to the Split(String, Int32) method then specifies a maximum of two elements in the returned array. The method returns a three-element string array.

string input = @"07/14/2007";
string pattern = @"(-)|(/)";
Regex regex = new Regex(pattern);
foreach (string result in regex.Split(input, 2))
   outputBlock.Text += String.Format("'{0}'", result) + "\n";
// The method returns an array of 3 elements, as follows:
//    '07'
//    '/'
//    '14/2007' 

If the regular expression can match the empty string, Split(String, 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 there are in the input string. Because the null string matches the beginning of the input string, a null string is inserted at the beginning of the returned array. This causes the tenth element to consist of the two characters at the end of the input string.

string input = "characters";
Regex regex = new Regex("");
string[] substrings = regex.Split(input, input.Length);
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}


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