0

I'm writing some simple Java code for handling strings, and I'm trying to understand the behavior of the String.split() method. I've come across this strange edge case behavior. Consider the three following examples.

String string = "";
String[] parts = string.split("-");

In this case, parts has a single element: the empty string.

String string = "-b";
String[] parts = string.split("-");

In this case, parts has two elements: the empty string, and the string "b".

So far so good. But then:

String string = "-";
String[] parts = string.split("-");

In this case, parts has zero elements!

My question is: in the last example, why does the array parts have zero elements, instead of two elements: two occurrences of the empty string? If it counts the empty string on the left side of the separator - in the second example, shouldn't it count the empty strings on either side in the third example?

Sambo
  • 189
  • 7
  • Yes, but it brings up a follow-up question. Why are trailing empty strings removed, but not empty strings at the beginning? – Sambo Mar 07 '21 at 00:55
  • 1
    I don't really know the rationale behind it. I just know that this is how it works. – Ivar Mar 07 '21 at 01:01
  • 1
    The real answer to all this is "that is how it is designed and documented". If you don't like the trailing-elements behavior, then call `split(delimiter, -1)` as per fhe doc. You might argue with the choice of which one was chosen as the default behavior, as would I, but it's a bit pointless if you have code to write. – user15187356 Mar 07 '21 at 01:03

0 Answers0