1

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

Community
  • 1
  • 1
gene b.
  • 10,512
  • 21
  • 115
  • 227

1 Answers1

2

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875