String. Split () function

Source: Internet
Author: User

We learned the string. Join function (http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx) last time, and used the string. Split function, so we can check the usage of this function online, for example:

 

#Used inString. SplitNotes for using methods to cut strings:
String. SplitIt provides us with a very flexible way to use,However, it is assumed that the service is improperly used.,May cause errors,RecentlyCode ReviewHour,Most people use this method.:
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"|: |"Separate strings,The objective is to get the Array:"A | B" And"C: d",But not directly foundSplit (string).,All are usedTochararray (),Obviously,The result is incorrect..

The correct method is:
String[]SS1=S.Split(New[] {"|: |"},Stringsplitoptions.None);
// SS1 [0]: A | B
// SS1 [1]: C: d

Next shard count,Indicates whether you are active or not.Remove emptyData.For example:"A | B |: | C: d |: |" Cutting,Returns three arrays.,The last element of the array is null."",Suppose we want to filter out these empty elements.,Number of available shards:Stringsplitoptions.Removeemptyentries That is: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 (below 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. Now I only analyze the method above: String [] split = words. split (New char [] {',', '. '}, 6); // return: {"1", "2", "3 ","", "4"} Why is there an empty "" Returned? 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 be regarded as" "empty to deal with the two consecutive ", "I think there is a space character. We are very grateful for pointing out the incorrect information.


 


String. Split () function

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.