Share via


Split Method (String, Int32)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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

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

Syntax

'Declaration
Public Function Split ( _
    s As String, _
    maxMatches As Integer _
) As String()
public string[] Split(
    string s,
    int maxMatches
)
public:
array<String^>^ Split(
    String^ s, 
    int maxMatches
)
member Split : 
        s:string * 
        maxMatches:int -> string[] 
public function Split(
    s : String, 
    maxMatches : int
) : String[]

Parameters

Return Value

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

Remarks

The Regex..::..Split methods are similar to the String..::..Split method, except that Regex..::..Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The maxMatches parameter specifies the maximum number of substrings into which the s string can be split; the last string contains the unsplit remainder of the string. A maxMatches value of zero provides the default behavior of splitting as many times as possible.

If multiple matches are adjacent to one another or if a match is found at the beginning or end of s, and the number of matches found is at least two less than maxMatches, an empty string is inserted into the array. That is, empty strings that result from adjacent matches or from matches at the beginning or end of the input string are counted in determining whether the number of matched substrings equals maxMatches.

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.

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.

.NET Framework Security

See Also

Reference

Regex Class

Split Overload

System.Text.RegularExpressions Namespace