简体   繁体   中英

Java Split Method Returns Empty Token

My source string is /study[2]/

When I do

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

the result is

[, study[2]]

tokens is String[2]

I was under the impression that the Split method removed all empty tokens? Why is it keeping the 1st one? I shouldn't get this according to Java String split removed empty values

The first sentence of the accepted answer to the question you linked:

split(delimiter) by default removes trailing empty strings from result array.

(my emphasis)

Yours isn't trailing (at the end), it's leading (at the beginning). split doesn't remove those. It also doesn't remove ones in the middle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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