在java 1.6中String.split的行为? [英] Behaviour of String.split in java 1.6?
问题描述
我的代码是:
String s = "1;;;; 23;;";
System.out.println(s.split(";").length);
并输出 5
。
split
的源代码是:
and gives as output 5
.
The source code of split
is:
public String[] split(String regex) {
return split(regex, 0);
}
并且文档说:
此方法的工作方式就像调用带有给定表达式的两参数
split(java.lang.String,int)方法和
limit参数一样零。因此,在结果数组中包含尾随空字符串不是
。
This method works as if by invoking the two-argument split(java.lang.String,int) method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
字符串boo:and:foo,例如,产生以下结果$ b带有以下表达式的$ b:
The string "boo:and:foo", for example, yields the following results with these expressions:
Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
如果我打印我有的字符串:
If I print the strings I have:
1
23
我不应该从这个 1获得; ;;; 23 ;;
类似 {1,,,,23,}
?
推荐答案
不,五是正确的,因为你引用的文档声明:
No, five is correct, as your quoted docs state:
因此,结果
数组中不包括尾随空字符串。
Trailing empty strings are therefore not included in the resulting array.
这就是为什么空数组末尾的字符串被省略。如果你想要空字符串,请按照Evgeniy Dorofeev的回答说明并指定-1的限制。
Which is why the empty strings at the end of the array are omitted. If you want the empty strings, do as Evgeniy Dorofeev's answer says and specify a limit of -1.
这篇关于在java 1.6中String.split的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!