Scene:
1. Sometimes you need to use some characters as the value of the merge storage, such as @ as the delimiter, [email protected]@323232, with a property to store these 3 values, when used to take out split on it.
2. The problem is sometimes a value is missing, such as the first value is not the case, @[email protected] 2nd, 3 values are not the case [email protected]@, of course, also expected to return 3 values, but the back 2 values are null characters on the line.
In fact, the result is not so, even if 2, 3 values are not in the case [email protected]@ also returns a 1th value only, the array size is 1. This makes the person very puzzled, obviously has the delimiter, why when cannot see? So be careful when you program again.
Split is an inert search, and if there is no value in the back if there is no result, but if there is only a 3rd value, it is true that the array size is 3, and the first two are empty strings. That is, split follows a principle that returns the results and the array
The index must be the least number of groups that correspond.
3. Java's practice will make the handwriting C + + split people crazy, do C + + switch Java to pay attention to.
4. That is, the split of string cannot get a fixed number of splits even if the number of delimiters is specified.
Situation 1.
String value = "\n\n2"; string[] names = Value.split ("\ n"), for (int i = 0; i < names.length; i++) {System.out.println ("I:" +names[i]);}
Output:
I:i:i:2
Situation 2.
String value = "2\n\n"; string[] names = Value.split ("\ n"), for (int i = 0; i < names.length; i++) {System.out.println ("I:" +names[i]);}
Output:
I:2
Scenario 3:
String value = "\ n"; string[] names = Value.split ("\ n"), for (int i = 0; i < names.length; i++) {System.out.println ("I:" +names[i]);}
Output: No output, number is 0.
Reference:
http://tjuking.iteye.com/blog/1507855
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[java]_[The Pit in the split of the primary]_[string]