字符串中,分隔符连续时,Java 的 String 类的 split 是如何分隔字符串的?
RTFM:public String[] split(String regex)再看 two-argument 的 split:
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.以上摘自 http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String,%20int)
所以如果你想获得所有结果,就使用 split('o', -1)
原发布于 https://www.zhihu.com/question/20318493/answer/14739491