输入为空时 String.split() 的行为 [英] Behaviour of String.split() when input is empty

查看:26
本文介绍了输入为空时 String.split() 的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题解释查询

有人可以解释以下两个输出的行为吗.

"".split(",").length

给出输出

1

在哪里

",".split(",").length

给出输出

<预><代码>0

解决方案

第一种情况,返回原始字符串,因为没有找到分隔符.

来自 API 文档:

<块引用>

如果表达式不匹配输入的任何部分,则结果数组只有一个元素,即这个字符串.

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.

From the 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆