String.Split ()
to use:
string[] Ss1 = S.split (new[] {"|:|"}, Stringsplitoptions.none); Ss1[0]: a| B //ss1[1]: c:d
The latter parameter, is whether the flag is automaticallyRemove Emptyof Data. For example:"a| b|:| c:d|:| " when doing a split, will return three arrays, The last element of the array is empty"", if we want to filter out these empty elements,, can use parameters:stringsplitoptions. removeemptyentries that: 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 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[] S Plit (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)
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)
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: {"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)
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"} do not leave empty element 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)
string[] split = words. Split (new string[] {",", "."}, 2, stringsplitoptions.removeemptyentries);//return: {"1", "2.3,,4"} do not leave empty element string[] Split = Wor Ds. Split (new string[] {",", "."}, 6, stringsplitoptions.none);//return: {"1", "2", "3", "", "4"} Leave empty elements
String.Split () function