Java中split方法的细节问题。
先看一段代码:
String a="a::::::";
String[] str=a.split(":");
// 返回长度为1
System.out.println(str.length);
你会觉得长度应该不是1,那是为什么呢? 看一下jdk中关于方法的说明。
split
public String[] split(String regex)
- Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument
splitmethod with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.The string "boo:and:foo", for example, yields the following results with these expressions:
Regex Result : { "boo", "and", "foo" } o { "b", "", ":and:f" }
发现红色的这句话很关键“结尾的空白字符串会被略去,不会包含在返回的结果数组中。
所以开始的代码返回的长度为1.
浙公网安备 33010602011771号