输入为空时String.split()的行为 [英] Behaviour of String.split() when input is empty
本文介绍了输入为空时String.split()的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于标题解释了查询
有人可以解释下面两个输出的行为。
Can somebody please explain the behavior of following two outputs.
"".split(",").length
给出输出
1
其中
",".split(",").length
给出输出
0
推荐答案
在第一种情况下,返回原始字符串,因为找不到分隔符。
In the first case, the original string is returned, because the separator is not found.
来自 API docs :
如果表达式不匹配输入的任何部分,那么结果数组只有一个元素,即这个字符串。
If the expression does not match any part of the input then the resulting array has just one element, namely this string.
这篇关于输入为空时String.split()的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文