1. Separated by string:
Using System.Text.RegularExpressions;
String str= "AAAJSBBBJSCCC";
String[] Sarray=regex.split (str, "JS", regexoptions.ignorecase);
foreach (String i in Sarray) Response.Write (i.tostring () + "<br>");
Output results:
Aaa
Bbb
Ccc
2. Separated by multiple characters:
The code is as follows |
Copy Code |
String str= "Aaajbbbscccjdddseee"; String[] Sarray=str. Split (new char[2] {' J ', ' s '}); foreach (String i in Sarray) Response.Write (i.tostring () + "<br>"); Output results: Aaa Bbb Ccc Ddd Eee |
3, separated by a single character:
The code is as follows |
Copy Code |
String S=abcdeabcdeabcde; String[] Sarray=s.split (c); foreach (String i in Sarray) Console.WriteLine (i.ToString ()); Output the following results: Ab Deab Deab De |
string[] arr = str. Split ("O");
This is a statement with a syntax error, the Split separator parameter should be char[] or string[] and should not be a string. The correct example:
The code is as follows |
Copy Code |
String str = "Technology"; Char[] Separator = {' O '}; string[] arr = str. Split (separator); |
using regular Expressions
First, you need to refer to a regular expression-related assembly in your program: using System.Text.RegularExpressions;
Then use the following method:
The code is as follows |
Copy Code |
String Content=agcsmallmacsmallgggsmallytx; String[]resultstring=system.text.regularexpressions. Regex.Split (Content,small,regexoptions.ignorecase) foreach (String i in resultstring) Console.WriteLine (i.ToString ()); Output the following results: Agc Mac Ggg Ytx |
#中Split分隔字符串时, if the delimiter is a character, the length of the character array returned is expected in general.
But when the delimiter is more than one character, such as Str. Split ("| | |"). ToCharArray ()), the returned character array may be more than expected length, the array will appear some values are empty string elements.
At this point we can use regular expressions to split, you may need to be familiar with regular expressions, but the general need to use the regular expressions are relatively simple:
The code is as follows |
Copy Code |
string[] arr = regex.split (str, @ "| | |", regexoptions.ignorecase);
|
Note: | In a regular expression, a reserved character, which needs to be escaped with "".
Example of breaking a carriage return in a split text field:
The code is as follows |
Copy Code |
string[] arr = regex.split (str, RN)
|
name |
Description |
String.Split (char[]) |
Returns a string array that contains the substrings in this instance delimited by the elements of the specified Char array. Supported by the. NET Compact Framework. |
String.Split (char[], Int32) |
Returns a string array that contains the substrings in this instance delimited by the elements of the specified Char array. parameter specifies the maximum number of substrings to return. |
String.Split (char[], stringsplitoptions) |
Returns a string array that contains the substrings in this string that are delimited by the elements of the specified Char array. parameter specifies whether to return an empty array element. |
String.Split (string[], stringsplitoptions) |
Returns a string array that contains the substrings in this string separated by the elements of the specified string array. parameter specifies whether to return an empty array element. |
String.Split (char[], Int32, stringsplitoptions) |
Returns a string array that contains the substrings in this string that are delimited by the elements of the specified Char array. parameter specifies the maximum number of substrings to return and whether to return an empty array element. |
String.Split (string[], Int32, stringsplitoptions) |
Returns a string array that contains the substrings in this string separated by the elements of the specified string array. parameter specifies the maximum number of substrings to return and whether to return an empty array element. |