Let's look at a simple example.
But its array length is 25, rather than 3. The following method replaces "[111cn.net]" with a special character, for example, $.
For example, if I want to split it according to [111cn.net],
| The code is as follows: |
Copy code |
|
String [] arrstr2 = str. Replace ("[111cn.net]", "$"). Split ('$ '); |
Check other methods.
, The simplest and most commonly used method. Open vs.net to create a console project by separating a specified character. Enter the following program in the Main () method.
| 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 |
2. Use multiple characters for segmentation
| The code is as follows: |
Copy code |
|
String s = "abcdeabcdeabcde"
String [] sArray1 = s. Split (new char [3] {'C', 'D', 'E '});
Foreach (string I in sArray1)
Console. WriteLine (I. ToString ());
Output the following results:
AB
AB
AB |
3. Use regular expressions
Add reference
| The code is as follows: |
Copy code |
|
Using System. Text. RegularExpressions;
String content = "agcsmallmacsmallgggsmallytx ";
String [] resultString = Regex. Split (content, "small", RegexOptions. IgnoreCase)
Foreach (string I in resultString)
Console. WriteLine (I. ToString ()); |
Output the following results:
Agc
Mac
Ggg
Ytx
There is also an uncommon method
The code is as follows:
| The code is as follows: |
Copy code |
String str = "reterry [111cn.net] is the webmaster of [111cn.net ";
String [] arrstr = str. split (new char [] {'[','s ', 'O','s', 'u', 'O', '8 ','. ', 'C', 'O', 'M','] '});
For (int I = 0; I <arrstr. Length; I ++)
{
Response. Write (arrstr [I]);
} |