String. Split () function

Source: Internet
Author: User

Note for splitting strings using the string. Split method in C:
String. Split provides us with a very flexible way to use it. However, improper use may cause errors. Most people recently used it in code review:
String s = "A | B |: | C: D ";
String [] ss = s. Split ("|: |". ToCharArray ());
// Ss [0]:
// Ss [1]: B
// Ss [2]:
// Ss [3]:
// Ss [4]: C
// Ss [5]: D
In fact, his intention is to use "|: |" to separate the string, the goal is to get the array: "A | B" and "C: D ", however, the overload of Split (string) is not directly found, and ToCharArray () is used. Obviously, the result is incorrect.

Correct usage:
String [] ss1 = s. Split (new [] {"|: |"}, StringSplitOptions. None );
// Ss1 [0]: A | B
// Ss1 [1]: C: D

The next parameter indicates whether to automatically remove empty data. for example, "A | B |: | C: D |: |" When splitting, three arrays are returned. The last element of the array is null "", if you want to filter out these empty elements, you can use the parameter StringSplitOptions. removeEmptyEntries: string [] ss1 = s. split (new [] {"|: |"}, StringSplitOptions. removeEmptyEntries );

Example:

S = "A | B |: | C: D |: | ";
String [] ss1 = s. Split (new [] {"|: |"}, StringSplitOptions. RemoveEmptyEntries );
// Ss1 [0]: "A | B"
// Ss1 [1]: "C: D"

Ss1 = s. Split (new [] {"|: |"}, StringSplitOptions. None );
// Ss1 [0]: "A | B"
// Ss1 [1]: "C: D"
// Ss1 [2]: ""

 

First, we can see that the String. Split 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 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)
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
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.
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"} retain null Elements
Personal Understanding: Split is String. A function opposite to Join is used to separate a string into arrays containing multiple fields. Currently, we only analyze the preceding method: string [] split = words. split (new Char [] {',', '. '}, 6); // return: {"1", "2", "3 ","", "4"} Why does the returned result contain an empty ""? At that time, I did not understand it. In fact, it is "," and ". "Two delimiters are used to split strings 1, 2.3, and 4. When the first separator after 3 is found," It indicates that the separator is separated, but it is followed by a separator ", "What should we do? This can only be treated as" "empty to handle two consecutive ", "can also be considered as a space character. We are very grateful to you for pointing out something wrong.

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.