In C,
I. The String. Split method provides the following six overload functions:
Name |
Description |
String. Split (Char []) |
Returns a String array containing a substring In the instance (separated by elements in the specified Char array. Supported by. NET Compact Framework. |
String. Split (Char [], Int32) |
Returns a String array containing a substring In the instance (separated by elements in the specified Char array. The parameter specifies the maximum number of substrings returned. |
String. Split (Char [], StringSplitOptions) |
Returns a String array that contains a substring (separated by elements in the specified Char array. The parameter specifies whether to return an empty array element. |
String. Split (String [], StringSplitOptions) |
Returns a String array that contains a substring (separated by elements in the specified String Array. The parameter specifies whether to return an empty array element. |
String. Split (Char [], Int32, StringSplitOptions) |
Returns a String array that contains a substring (separated by elements in the specified Char array. The 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 a substring (separated by elements in the specified String Array. The parameter specifies the maximum number of substrings to return and whether to return an empty array element. |
The following are examples:
1. String. Split (Char [])
String str = "aaatbbscctdd ";
String [] strArray = str. split (new char [] {'T'}); or string [] strArray = str. split ('T'); // single-character cut (result: "aaa" "bbscc" "dd ")
String [] strArray = str. split (new char [] {'T', 's'}); // multi-byte cutting (result: "aaa" "bb" "cc" "dd ")
2. String. Split (Char [], Int32)
String str = "aaatbbscctdd ";
String [] strArray = str. Split (new char [] {'T', 2}); // cut only two parts (result: "aaa" "bbscctdd ")
3. String. Split (Char [], StringSplitOptions)
String str = "aaatbbscctddt ";
String [] strArray = str. split (new char [] {'T', StringSplitOptions. removeEmptyEntries}); // remove the null element (result: "aaa" "bbscc" "dd ")
String [] strArray = str. split (new char [] {'T', StringSplitOptions. none}); // retain the null element (result: "aaa" "bbscc" "dd")
4. String. Split (String [],StringSplitOptions)
String str = "aaatbbscctddt ";
String [] strArray = str. split (new String [] {"t", StringSplitOptions. removeEmptyEntries}); // remove the null element (result: "aaa" "bbscc" "dd ")
String [] strArray = str. split (new String [] {"t", StringSplitOptions. none}); // retain the null element (result: "aaa" "bbscc" "dd")
5. String. Split (Char [], Int32, StringSplitOptions)
String str = "aaatbbscctddt ";
String [] strArray = str. split (new char [] {'T', 2, StringSplitOptions. removeEmptyEntries}); // cut it into two parts and remove the empty element (result: "aaa" "bbscctddt ")
String [] strArray = str. split (new char [] {'T', 2, StringSplitOptions. none}); // cut to 2 parts and retain null elements (result: "aaa" "bbscctddt")
6. String. Split (String [], Int32, StringSplitOptions)
String str = "aaatbbscctddt ";
String [] strArray = str. split (new String [] {"t", 2, StringSplitOptions. removeEmptyEntries}); // cut it into two parts and remove the empty element (result: "aaa" "bbscctddt ")
String [] strArray = str. split (new String [] {"t", 2, StringSplitOptions. none}); // cut to 2 parts and retain null elements (result: "aaa" "bbscctddt")
Ii. Regular Expression matching split strings provide five overload functions:
Name |
Description |
Regex. Split (String) |
Splits the specified input string at the position defined by the regular expression mode specified by the Regex constructor. Supported by. NET Compact Framework. |
Regex. Split (String, Int32) |
The position defined by the regular expression specified by the Regex constructor splits the specified input string for the specified maximum number of times. Supported by. NET Compact Framework. |
Regex. Split (String, String) |
Splits the input string at the position defined by the regular expression mode. Supported by. NET Compact Framework. |
Regex. Split (String, Int32, Int32) |
The position defined by the regular expression specified in the Regex constructor starts from the specified character position in the input string and splits the specified string to the specified maximum number of times. Supported by. NET Compact Framework. |
Regex. Split (String, String, RegexOptions) |
Splits the input string at the position defined by the specified regular expression mode. You can specify options to modify matching behaviors. Supported by. NET Compact Framework. |
Using System. Text. RegularExpressions;
1. Regex. Split (String, String)
String str = "aaatsbbtscctsdd ";
Stirng [] strArray = Regex. Split (str, "ts"); // Regular Expression matching (result: "aaa" bb "" cc "" dd ");
2. Regex. Split (String, String)
String str = "aaatsbbtscctsdd ";
Stirng [] strArray = Regex. split (str, "ts", RegexOptions. ignoreCase); // Regular Expression matching (result: "aaa" "bb" "cc" "dd ");
Iii. Application
1. Single-character splitting
String str = "aaatbbscctdd ";
String [] strArray = str. split (new char [] {'T'}); or string [] strArray = str. split ('T'); // single-character cut (result: "aaa" "bbscc" "dd ")
2. Multi-character splitting
String str = "aaatbbscctdd ";
String [] strArray = str. split (new char [] {'T', 's'}); // multi-byte cutting (result: "aaa" "bb" "cc" "dd ")
3. String splitting
Using System. Text. RegularExpressions;
String str = "aaatsbbtscctsdd ";
Stirng [] strArray = Regex. split (str, "ts", RegexOptions. ignoreCase); // Regular Expression matching and (result: "aaa" "bb" "cc" "dd ");
Or
String str = "aaatsbbtscctsdd ";
String [] strArray = str. Split (new string [] {"ts"}); // string cutting (result: "aaa" "bb" "cc" "dd ")
Note:
1. string. the Split method does not overload String. split (stirng []) method, that is, str. split ("t") or str. split (new string [] {"ts", "dd"}) is incorrect.
2. in addition to single-character splitting, String can be used. except Split ('T'), all others must use String. split (new char [] {}) format, otherwise the compilation will fail