We often use split when splitting strings. Next we will learn the techniques and methods for splitting strings.
1. Use string. Split to separate a single character
String. Split Method
Namespace: SystemProgramSet: mscorlib (in mscorlib. dll)
The returned string array contains the child strings in this instance (separated by elements in the specified string or Unicode character array ).
The string array returned by Split (char []) contains the child strings in this instance (separated by elements in the specified UNICODE character array ).
The string array returned by Split (char [], int32) contains the child strings in this instance (separated by elements of the specified UNICODE character array ). The parameter specifies the maximum number of substrings returned. The string array returned by Split (char [], stringsplitoptions) contains the substrings in this string (separated by elements in the array of specified Unicode characters ). The parameter specifies whether to return an empty array element.
The string array returned by Split (string [], stringsplitoptions) contains the substrings in this string (separated by elements in the specified String Array ). The parameter specifies whether to return an empty array element.
The string array returned by Split (char [], int32, stringsplitoptions) contains the substrings in this string (separated by elements in the array of specified Unicode characters ). The parameter specifies the maximum number of substrings to return and whether to return an empty array element.
The string array returned by Split (string [], int32, stringsplitoptions) contains the sub-string (separated by elements of the specified String Array ). The parameter specifies the maximum number of substrings to return and whether to return an empty array element.
Example 1:
String S = abcdeabcdeabcde; string [] sarray = S. split ('C'); foreach (string I in sarray) console. writeline (I. tostring (); output the following result: AB Deab de
Example 2:
String S = "abcdeabcdeabcde"; string [] sarray1 = S. split (New char [3] {'C', 'D', 'E'}); foreach (string I in sarray1) console. writeline (I. tostring (); the following result can be output: AB
Example 3: String str1 = I'm a *** * teacher; string [] str2; str1 = str1.replace (*****, *); str2 = str1.split (*); foreach (string I in str2) console. writeline (I. tostring ());
Example 4:
String str1 = I'm a ********************** teacher. The result I want to display is: I am a teacher. If I use the fourth method above, the following error will occur: I am a teacher. There is a space in the middle of the output, so the output result is not the expected result, which returns to the regular expression, then you can use the fifth method below: string str1 = I'm a ********************* teacher; string [] str2 = system. text. regularexpressions. regEx. split (str1, @ [*] +); foreach (string I in str2) console. writeline (I. tostring (); here we use [*] + to skillfully accomplish our goal.
Example 5:
String strbreak = "|"; string STR = "111" + strbreak + "222" + strbreak + "333" + strbreak + "444"; string [] arr1 = Str. split ('|'); foreach (string I in arr1) response. write (I. tostring () + "<br> \ n ");
Result (meeting requirements) ====================== 111 <br> 222 <br> 333 <br> 444 <br>
Example 6:
Tring strbreak = "|"; string STR = "111" + strbreak + "222" + strbreak + "333" + strbreak + "444"; char [] arrchr = strbreak. tochararray (); string [] arr1 = Str. split (arrchr); // string [] arr1 = Str. split (New char [2] {'|', '|'}); // or write foreach (string I in arr1) response. write (I. tostring () + "<br> \ n ");
Result (a few characters are printed <br>, which does not meet the requirements) ==================== 111 <br> 222 <br> 333 <br> 444 <br>
Example 7:
Use regular expressions to process multiple characters.
String strbreak = "|"; string STR = "111" + strbreak + "222" + strbreak + "333" + strbreak + "444"; string [] arr1 = RegEx. split (STR, @ "\ |", regexoptions. ignorecase); // string [] arr1 = RegEx. split (STR, "http://www.cnblogs.com/xiachufeng/admin/file://u007c//u007C%22,RegexOptions.IgnoreCase); // special characters can also be replaced with a http://www.cnblogs.com/xiachufeng/admin/file://u007c/ |
Foreach (string I in arr1) response. Write (I. tostring () + "<br> \ n ");
Result (meeting requirements) ====================== 111 <br> 222 <br> 333 <br> 444 <br>
2. Use RegEx. Split to separate multiple characters
Namespace: system. Text. regularexpressions Assembly: system (in system. dll)
The RegEx. split method is similar to the split method, but the latter method splits the string at the separator determined by the regular expression rather than a group of characters. If count is specified, the string is split into Count strings at most (the last string contains the remaining unsplit parts of the string ); if the Count value is zero, the default splitting behavior is used (that is, to split as many as possible ). If startat is specified, the first separator is searched to start the operation from the specified position (for example, this can be used to skip leading blank spaces ).
If a capture group is used in the RegEx. Split expression, the generated string array contains the capture group. The following example generates the array items "one", "-", "two", "-", and "banana ".
RegEx r = new RegEx ("(-)"); // split on hyphens. String [] S = R. Split ("one-two-banana ");
If the regular expression can match an empty string (for example, x *), split splits the string into an array of strings composed of a single character, because an empty string separator can be found at each position.
The static split method is equivalent to constructing a RegEx object using the specified regular expression mode and calling the instance method split. Static methods are provided to use regular expressions independently and independently, without explicitly creating a RegEx object.
Example 1:
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> ");