Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Regex Class
Regex Methods
Split Method
 Split Method (String)
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Regex..::.Split Method (String)

Splits the specified input string at the positions defined by a regular expression pattern specified in the Regex constructor.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Function Split ( _
    input As String _
) As String()
Visual Basic (Usage)
Dim instance As Regex
Dim input As String
Dim returnValue As String()

returnValue = instance.Split(input)
C#
public string[] Split(
    string input
)
Visual C++
public:
array<String^>^ Split(
    String^ input
)
JScript
public function Split(
    input : String
) : String[]

Parameters

input
Type: System..::.String
The string to split.

Return Value

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

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

The Regex..::.Split(String) method is similar to the String..::.Split(array<Char>[]()[]) method, except this method splits the string at a delimiter determined by a regular expression instead of a set of characters. The string is split as many times as possible. If no delimiter is found, the return value contains one element whose value is the original input parameter string.

If multiple matches are adjacent to one another, an empty string is inserted into the array. For example, splitting a string on a single hyphen causes the returned array to include an empty string in the position where two adjacent hyphens are found, as the following code shows.

Visual Basic
Dim regex As Regex = New Regex("-")         ' Split on hyphens.
Dim substrings() As String = regex.Split("plum--pear")
For Each match As String In substrings
   Console.WriteLine("'{0}'", match)
Next
' The method writes the following to the console:
'    'plum'
'    ''
'    'pear'      
C#
Regex regex = new Regex("-");         // Split on hyphens.
string[] substrings = regex.Split("plum--pear");
foreach (string match in substrings)
{
   Console.WriteLine("'{0}'", match);
}
// The method writes the following to the console:
//    'plum'
//    ''
//    'pear'      

If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. For example, splitting the string " plum-pear" on a hyphen placed within capturing parentheses adds a string element that contains the hyphen to the returned array.

Visual Basic
Dim regex As Regex = New Regex("(-)")          ' Split on hyphens.
Dim substrings() As String = regex.Split("plum-pear")
For Each match As String In substrings
   Console.WriteLine("'{0}'", match)
Next
' The method writes the following to the console:
'    'plum'
'    '-'
'    'pear'      
C#
Regex regex = new Regex("(-)");         // Split on hyphens.
string[] substrings = regex.Split("plum-pear");
foreach (string match in substrings)
{
   Console.WriteLine("'{0}'", match);
}
// The method writes the following to the console:
//    'plum'
//    '-'
//    'pear'      

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 the .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 elements of a date, including the date delimiters, from a date string. The first set of capturing parentheses captures the hyphen, while the second set captures the forward slash. If the example code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the slash characters; if it is compiled and run under the .NET Framework 2.0, it includes them.

Visual Basic
Dim input As String = "07/14/2007"   
Dim pattern As String = "(-)|(/)"
Dim regex As Regex = New Regex(pattern)
For Each result As String In regex.Split(input) 
   Console.WriteLine("'{0}'", result)
Next
' In .NET 1.0 and 1.1, the method returns an array of
' 3 elements, as follows:
'    '07'
'    '14'
'    '2007'
'
' In .NET 2.0, the method returns an array of
' 5 elements, as follows:
'    '07'
'    '/'
'    '14'
'    '/'
'    '2007' 
C#
string input = @"07/14/2007";   
string pattern = @"(-)|(/)";
Regex regex = new Regex(pattern);
foreach (string result in regex.Split(input)) 
{
   Console.WriteLine("'{0}'", result);
}
// Under .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '14'
//    '2007'
//
// Under .NET 2.0, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007' 

If the regular expression can match the empty string, Split(String) will split the string into an array of single-character strings because the empty string delimiter can be found at every location. For example:

Visual Basic
Dim input As String = "characters"
Dim regex As New Regex("")
Dim substrings() As String = regex.Split(input)
Console.Write("{")
For ctr As Integer = 0 to substrings.Length - 1
   Console.Write(substrings(ctr))
   If ctr < substrings.Length - 1 Then Console.Write(", ")
Next
Console.WriteLine("}")
' The example produces the following output:   
'    {, c, h, a, r, a, c, t, e, r, s, }
C#
string input = "characters";
Regex regex = new Regex("");
string[] substrings = regex.Split(input);
Console.Write("{");
for(int ctr = 0; ctr < substrings.Length; ctr++)
{
   Console.Write(substrings[ctr]);
   if (ctr < substrings.Length - 1)
      Console.Write(", ");
}
Console.WriteLine("}");
// The example produces the following output:   
//    {, c, h, a, r, a, c, t, e, r, s, }

Note that the returned array also includes an empty string at the beginning and end of the array.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Empty String elements.      RobertRFreeman   |   Edit   |   Show History
Note that an empty string will be included at the beginning or end if the expression matches at either extreme. The documentation above makes this seem specific to an empty string filter criteria only.

I.e: a filter of '1' on '1221' would return ('', '22', '')
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker