简体   繁体   English

Java拆分方法返回空令牌

[英]Java Split Method Returns Empty Token

My source string is /study[2]/ 我的源字符串是/study[2]/

When I do 当我做

String[] tokens = itemPath.split("/");

the result is 结果是

[, study[2]]

tokens is String[2] 令牌是字符串[2]

I was under the impression that the Split method removed all empty tokens? 我的印象是Split方法删除了所有空令牌? Why is it keeping the 1st one? 为什么要保持第一个呢? I shouldn't get this according to Java String split removed empty values 我不应该根据Java字符串拆分的删除空值

The first sentence of the accepted answer to the question you linked: 您所链接问题的答案的第一句:

split(delimiter) by default removes trailing empty strings from result array. 默认情况下split(delimiter)从结果数组中删除结尾的空字符串。

(my emphasis) (我的重点)

Yours isn't trailing (at the end), it's leading (at the beginning). 您的并不落后(最后),而是领先 (开始)。 split doesn't remove those. split不会删除那些。 It also doesn't remove ones in the middle. 它也不会删除中间的那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM