[英]Behaviour of String.split() when input is empty
As the title explains the query 由于标题解释了查询
Can somebody please explain the behavior of following two outputs. 有人可以解释下面两个输出的行为。
"".split(",").length
gives output 给出输出
1
where as 在哪里
",".split(",").length
gives output 给出输出
0
In the first case, the original string is returned, because the separator is not found. 在第一种情况下,返回原始字符串,因为找不到分隔符。
If the expression does not match any part of the input then the resulting array has just one element, namely this string.
如果表达式与输入的任何部分都不匹配,那么结果数组只有一个元素,即该字符串。
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String,%20int) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String,%20int)
Trailing empty strings are discarded. 尾随空字符串被丢弃。
Try: 尝试:
"Foo,".split(",").length // should be 1
",foo".split(",").length // should be 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.