C # split usage

Source: Internet
Author: User
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 (',')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.