Regex.Split Method (String, Int32, Int32)
Assembly: System (in system.dll)
The string to be split.
The maximum number of times the split can occur.
The character position in the input string where the search will begin.
Return ValueAn array of strings.
input is a null reference (Nothing in Visual Basic).
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.
However, when the regular expression pattern includes multiple sets of capturing parentheses, the behavior of this method depends on the version of the .NET Framework. In .NET Framework versions 1.0 and 1.1, if a match is not found within the first set of capturing parentheses, captured text from additional capturing parentheses is not included in the returned array. In the .NET Framework version 2.0, all captured text is also 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. If the sample code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the vertical bar characters; if it is compiled and run under the .NET Framework 2.0, it includes them.
If the regular expression can match the empty string, Split 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.
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.