In Java, a feature that separates a string like "1|2|3|4" into an array
This problem can be stumped by more than 10 years of development experience. NET code farmer?
// Java code String s= "1|2|3"; string[] Array=s.split ("|");
As a result, the array that comes out is this ghost look 1,|,2,|,3
Take a closer look at the name of the parameter that split passed in, a regex, a regular expression, an epiphany, to escape the regular expression ...
// Java code String s= "1|2|3"; string[] Array=s.split ("\\|");
After the problem is solved, continue to hand the cheap research this split, looking for. NET inside my favorite option where is Stringsplitoptions.removeemptyentries?
Do an experiment.
// Java code String s= ",,,"; string[] Array=s.split (","); // The result of array is
Does the default with Removeemptyentries feature?
Then try again ...
// Java code String s= ",,, 3,,,"; string[] Array=s.split (","); // "1", "2", "" "," "," 3 "
What the hell is the result??? Removeemptyentries of the tail method, automatically discard the trailing empty elements ? Old yards with. NET can't find the channel ah ...
Well, even so, the empty elements in the middle I can filter, if I do not want to abandon the empty tail of the elements can be the whole?
Baidu has a half-day, found a solution, if you want to keep the tail empty elements, to pass Limit=-1
// Java code String s= ",,, 3,,,"; string[] Array// Incoming-1, keep trailing empty elements // "1", "2", "" "", "3", "" "," ""
Okay, I'll go on to the second parameter, limit.
Public int limit)
The literal meaning should be to limit the number of split returned array elements,
The meaning of this limit is the same as. NET is very similar to the Count parameter
// C # code Public int count, stringsplitoptions options)
If limit or count=n, indicates that the number of matches is n-1
// C # code String s = Span style= "color: #800000;" > " 1|2|3| | | 4 ; string[] Array = S.split (new string[] { " /span>| }, 2 // The result of the array is {"1", "2|3| | | 4 "}
// Java code String s= ",,, 3,,,"; string[] Array=s.split (",", 2); // result of Array {"1", "2,,, 3,,,"}
See here, I think I understand.
And I'm depressed by the tests below.
// Java code String s1= ""; String[] array1=s1.split (","); // The result of the array1 is {""} array with 1 empty elements String s2= ","; String[] array2=s2.split (","); // The result of the array2 is that the {} array contains 0 elements
It turns out I really want 12315 complaints ...
. NET to java.06. The difference between the split of a string