Here I will illustrate several common spli methods in C #. 1 , Separated by strings:
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 result:
Aaa
Bbb
CCC
2 , Separated by multiple characters:
String Str = " Aaajbbbscccjdddseee " ; String [] Sarray = Str. Split ( New Char [ 2 ] {'J','S'} );
Foreach ( String I In Sarray) response. Write (I. tostring () + " <Br> " );
Output result:
Aaa
Bbb
CCC
Ddd
Eee
3 , Separated by a single character:
String Str = " Aaajbbbjccc " ;
String [] Sarray = Str. Split ( ' J ' );
Foreach ( String I In Sarray) response. Write (I. tostring () + " <Br> " );
Output result:
Aaa
Bbb
CCC
//////////////////////////////////////// /// // String [] arr = Str. split ("O ");
This is a statement with a syntax error,The separator parameter of split should beChar[] Or string []. It should not be a string. Correct example:
String STR = "technology ";
Char [] separator = {'O '};
String [] arr = Str. split (separator ); //////////////////////////////////////// //// // string. the split method has six overload functions: Program Code 1) Public String [] Split (Params char [] separator) 2) Public String [] Split (char [] separator, int count)
3) Public String [] Split (char [] separator, stringsplitoptions options)
4) Public String [] Split (string [] separator, stringsplitoptions options)
5) Public String [] Split (char [] separator, int count, stringsplitoptions options)
6) Public String [] Split (string [] separator, int count, stringsplitoptions options) Below we use some instances to explain how to use (the following string words = "1, 2.3 ,, 4 ";):
1. Public String [] Split (Params char [] separator) Program code string [] split = words. split (New char [] {','}); // return value: {"1", "2.3", "", "4 "}
String [] split = words. split (New char [] {',', '. '}); // return: {"1", "2", "3", "", "4 "}
2. Public String [] Split (char [] separator, int count) Itpub personal space, N: H! C0m/s3u \ u0002p Program code string [] split = words. split (New char [] {',', '. '}, 2); // return: {"1", "2.3, 4 "}
String [] split = words. split (New char [] {',', '. '}, 6); // return: {"1", "2", "3", "", "4 "}
3. Public String [] Split (char [] separator, stringsplitoptions options) Program code string [] split = words. split (New char [] {',', '. '}, stringsplitoptions. removeemptyentries); // return value: {"1", "2", "3", "4"} empty element not retained
String [] split = words. split (New char [] {',', '. '}, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
4. Public String [] Split (string [] separator, stringsplitoptions options) Program code string [] split = words. split (New String [] {",", ". "}, stringsplitoptions. removeemptyentries); // return value: {"1", "2", "3", "4"} empty element not retained
\ U0002w1i + CH % ^ \ u0017} 0 String [] split = words. split (New String [] {",", ". "}, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
5. Public String [] Split (char [] separator, int count, stringsplitoptions options) Program code string [] split = words. split (New char [] {',', '. '}, 2, stringsplitoptions. removeemptyentries); // return value: {"1", "2.3, 4"} empty elements are not retained. Itpub personal space 1 K; e \ u0007f \ u0008f} \ u0011c n
String [] split = words. split (New char [] {',', '. '}, 6, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
6. Public String [] Split (string [] separator, int count, stringsplitoptions options) Program code string [] split = words. split (New String [] {",", ". "}, 2, stringsplitoptions. removeemptyentries); // return value: {"1", "2.3, 4"} empty elements are not retained.
String [] split = words. split (New String [] {",", ". "}, 6, stringsplitoptions. none); // return: {"1", "2", "3 ","", "4"} to retain null elements, note that the Public String [] Split (string [] separator) function is not overloaded, so we cannot be like VB. net. split (","), but can only use words. split (',')
Here I will illustrate several common spli methods in C #. 1 , Separated by strings:
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 result:
Aaa
Bbb
CCC
2 , Separated by multiple characters:
String Str = " Aaajbbbscccjdddseee " ; String [] Sarray = Str. Split ( New Char [ 2 ] {'J','S'} );
Foreach ( String I In Sarray) response. Write (I. tostring () + " <Br> " );
Output result:
Aaa
Bbb
CCC
Ddd
Eee
3 , Separated by a single character:
String Str = " Aaajbbbjccc " ;
String [] Sarray = Str. Split ( ' J ' );
Foreach ( String I In Sarray) response. Write (I. tostring () + " <Br> " );
Output result:
Aaa
Bbb
CCC
//////////////////////////////////////// /// // String [] arr = Str. split ("O ");
This is a statement with a syntax error,The separator parameter of split should beChar[] Or string []. It should not be a string. Correct example:
String STR = "technology ";
Char [] separator = {'O '};
String [] arr = Str. split (separator ); //////////////////////////////////////// //// // string. the split method has six overload functions: program code 1) Public String [] Split (Params char [] separator) 2) Public String [] Split (char [] separator, int count)
3) Public String [] Split (char [] separator, stringsplitoptions options)
4) Public String [] Split (string [] separator, stringsplitoptions options)
5) Public String [] Split (char [] separator, int count, stringsplitoptions options)
6) Public String [] Split (string [] separator, int count, stringsplitoptions options) Below we use some instances to explain how to use (the following string words = "1, 2.3 ,, 4 ";):
1. Public String [] Split (Params char [] separator) Program code string [] split = words. split (New char [] {','}); // return value: {"1", "2.3", "", "4 "}
String [] split = words. split (New char [] {',', '. '}); // return: {"1", "2", "3", "", "4 "}
2. Public String [] Split (char [] separator, int count) Itpub personal space, N: H! C0m/s3u \ u0002p Program code string [] split = words. split (New char [] {',', '. '}, 2); // return: {"1", "2.3, 4 "}
String [] split = words. split (New char [] {',', '. '}, 6); // return: {"1", "2", "3", "", "4 "}
3. Public String [] Split (char [] separator, stringsplitoptions options) Program code string [] split = words. split (New char [] {',', '. '}, stringsplitoptions. removeemptyentries); // return value: {"1", "2", "3", "4"} empty element not retained
String [] split = words. split (New char [] {',', '. '}, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
4. Public String [] Split (string [] separator, stringsplitoptions options) Program code string [] split = words. split (New String [] {",", ". "}, stringsplitoptions. removeemptyentries); // return value: {"1", "2", "3", "4"} empty element not retained
\ U0002w1i + CH % ^ \ u0017} 0 String [] split = words. split (New String [] {",", ". "}, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
5. Public String [] Split (char [] separator, int count, stringsplitoptions options) Program code string [] split = words. split (New char [] {',', '. '}, 2, stringsplitoptions. removeemptyentries); // return value: {"1", "2.3, 4"} empty elements are not retained. Itpub personal space 1 K; e \ u0007f \ u0008f} \ u0011c n
String [] split = words. split (New char [] {',', '. '}, 6, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
6. Public String [] Split (string [] separator, int count, stringsplitoptions options) Program code string [] split = words. split (New String [] {",", ". "}, 2, stringsplitoptions. removeemptyentries); // return value: {"1", "2.3, 4"} empty elements are not retained.
String [] split = words. split (New String [] {",", ". "}, 6, stringsplitoptions. none); // return: {"1", "2", "3 ","", "4"} to retain null elements, note that the Public String [] Split (string [] separator) function is not overloaded, so we cannot be like VB. net. split (","), but can only use words. split (',')