您的位置:首页 > 编程语言 > Java开发

Java中split方法的细节问题。

2011-03-16 10:42 411 查看
先看一段代码:

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
split
method 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:


RegexResult
:{ "boo", "and", "foo" }
o{ "b", "", ":and:f" }


发现红色的这句话很关键“结尾的空白字符串会被略去,不会包含在返回的结果数组中。

所以开始的代码返回的长度为1.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: