Before Java 8 when we split on empty string like
String[] tokens = "abc".split("");
split mechanism would split in places marked with |
|a|b|c|
because empty space ""
exists before and after each character. So as result it would generate at first this array
["", "a", "b", "c", ""]
and later will remove trailing empty strings (because we didn't explicitly provide negative value to limit
argument) so it will finally return
["", "a", "b", "c"]
In Java 8 split mechanism seems to have changed. Now when we use
"abc".split("")
we will get ["a", "b", "c"]
array instead of ["", "a", "b", "c"]
.
My first guess was that maybe now leading empty strings are also removed just like trailing empty strings.
But this theory fails, since
"abc".split("a")
returns ["", "bc"]
, so leading empty string was not removed.
Can someone explain what is going on here? How rules of split
have changed in Java 8?