因为在String#split()
方法,尾随空字符串将不包含在数组结果中。
String str =",,,,,";
String str2=",,,,, ";
As str.split(",")
将应该给你[, , , , ,]
, 返回[]
相反,因为你有一个尾随空字符串 there.
不同于str2.split(",")
它会给你
[, , , , , ]
^ //note the whitespace element
你可以尝试
System.out.println(",,,,, ,,,,".split(",").length);
这仍然会给你输出:6
,即[, , , , , ]
因为之后空白,您所拥有的只是尾随空字符串(因此不包含在数组结果中)
Note:您可以通过指定限制来保留尾随空字符串:
System.out.println(str.split(",", -1).length);
^ limit
Output :6
检查String#split 文档 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split更多细节