// Construct result
int resultSize = matchList.size();
if (limit == 0)
while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
resultSize--;
String[] result = new String[resultSize];
return matchList.subList(0, resultSize).toArray(result);
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.
我想说,在JavaScript里,分出的是["b","",":and:f","",""],比较贴近我们的思维,但是Java里面split的一段源码这样写道:
可以看到while循环里面,如果结果集的最后元素是"",它会把它们一个一个地删除,这就是你所看到的结果的根本原因。
详细可以去看下我的博文:Java split源码分析
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String, int)
看
split
的第二个参数limit
你要的结果可以传
-1
得到foo.split("o",-1);