C # Split method

Source: Internet
Author: User

The string.split method has 6 overloaded 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 have some examples to illustrate how to use (the following string words = "1,2.3,,4";):

1. Public string[] Split (params char[] separator)--split the string into the contents of the character array, all displayed

string[] split = words. Split (new char[] {', '});//return: {"1", "2.3", "", "4"}
string[] split = words. Split (new char[] {', ', '. '}); /return: {"1", "2", "3", "", "4"}


2. Public string[] Split (char[] separator, int count)--splits the string by the contents of a character array, splits it into count, and then displays it completely.

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)--splits the string by the contents of the character array to determine whether to preserve the empty element.

string[] split = words. Split (new char[] {', ', '. '}, stringsplitoptions.removeemptyentries);//return: {"1", "2", "3", "4"} do not preserve empty elements
string[] split = words. Split (new char[] {', ', '. '}, Stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Leave empty elements


4. Public string[] Split (string[] separator, stringsplitoptions options)--splits the string into the contents of a string array to determine whether to preserve empty elements. Keep None, do not retain removeemptyentries

string[] split = words. Split (new string[] {",", "."}, stringsplitoptions.removeemptyentries);//return: {"1", "2", "3", "4"} do not preserve empty elements
string[] split = words. Split (new string[] {",", "."}, Stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Leave empty elements


5. Public string[] Split (char[] separator, int count, stringsplitoptions options)

string[] split = words. Split (new char[] {', ', '. '}, 2, stringsplitoptions.removeemptyentries);//return: {"1", "2.3,,4"} does not preserve empty elements
string[] split = words. Split (new char[] {', ', '. '}, 6, stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Leave empty elements


6. Public string[] Split (string[] separator, int count, stringsplitoptions options) splits the string into count by the contents of the string array, deciding whether to preserve empty elements

string[] split = words. Split (new string[] {",", "."}, 2, stringsplitoptions.removeemptyentries);//return: {"1", "2.3,,4"} does not preserve empty elements
string[] split = words. Split (new string[] {",", "."}, 6, stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Leave empty elements

C # Split method

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.