String. split method of C #

Source: Internet
Author: User

String. SplitThe method has six overload functions:

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 examples to illustrate how to use (the following string words = "1, 2.3, 4 ";):

1. Public String [] Split (Params char [] separator)

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)

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)

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)

String [] split = words. split (New String [] {",", ". "}, stringsplitoptions. removeemptyentries); // return value: {"1", "2", "3", "4"} empty element not retained
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)

String [] split = words. split (New char [] {',', '. '}, 2, stringsplitoptions. removeemptyentries); // return value: {"1", "2.3, 4"} empty elements are not retained.
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)

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"} retain null Elements

Note that there is no overload function Public String [] Split (string [] separator), so we cannot be like VB. net. split (","), but can only use words. split (',')! Many people are wondering why we can change double quotes to single quotes? After reading the overload function above, I should know the answer. ^_^

Related Article

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.