We learned the String.Join function (http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx) last time, When the String.Split function is used, it is possible to check the usage of the function on the Internet such as the following:
#used instring. SplitConsiderations for cutting strings by Method:
String. Splitprovides us with a very flexible way to use, but assuming improper use, can cause errors, recently in doingCode Reviewwhen, To see how most people use it.:
string s="a| b|:| C:d ";
string[]SS=s.Split("|:|".ToCharArray());
//ss[0]: A
//ss[1]: B
//ss[2]:
//ss[3]:
//ss[4]: C
//ss[5]: D
in fact, his intention was to use"|:|"separating the strings , The goal is to get the array : "a| B" and the"c:d", However, without directly finding the overload of the Split (string) , all of which is using ToCharArray (), it is very clear that the result is wrong .
The correct way to use this is :
string [] ss1 = s. Split (new[] {"|:|"}, stringsplitoptions. None);
//ss1[0]: a| B
//ss1[1]: c:d
after a number of parameters, is the sign of self-initiativeRemove Emptyof Data. like:"a| b|:| c:d|:| " when doing cutting, will return three arrays, The last element of the array is empty"", Let's say we want to filter out these empty elements., ability to use a number of parameters:stringsplitoptions. removeemptyentries that: string[] ss1 = s. Split (new[] {"|:|"}, stringsplitoptions. removeemptyentries);
Sample 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 look at the String.Split method with 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 (below string words = "1 , 2.3,,4 ";):
1. Public string[] Split (params char[] separator)
Program code 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)
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: {"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)
Program code 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)
Program code 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)
Program code 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
Personal understanding: Split is the opposite of String.Join's function it is to separate a string into an array with multiple fields I am now only analyzing one of the methods above: string[] Split = words. Split (new char[] {', ', '. '}, 6);//return: {"1", "2", "3", "", "4"} Why returned with an empty "", and I did not understand the fact that it is to "," and ". "Two separators to split the string 1,2.3,,4 when I find the first" after 3, "the time to explain here is to separate, but it is also a delimiter after it", "How to do this is just as" "to deal with the empty you can disguise the 1,2.3,,4 of two consecutive", " Feel that there is a space "" can also. Please help us to point out the wrong place.
String.Split () function