Split on an empty string returns an array of size 1 :
scala> "".split(',')
res1: Array[String] = Array("")
Consider that this returns empty array:
scala> ",,,,".split(',')
res2: Array[String] = Array()
Please explain :)
If you split an orange zero times, you have exactly one piece - the orange.
"orange".split(','), but isn't obviously relevant for splitting empty strings. If I split my lack of orange zero times, I still have no orange; do we represent that as an empty list of no-oranges, a list of exactly one no-orange, a list of twelve no-oranges, or what? It's not a question of what we end up with, but how we represent it.0/0?The Java and Scala split methods operate in two steps like this:
",,,".split(",") returns empty array.According to this, the result of "".split(",") should be an empty array because of the second step, right?
It should be. Unfortunately, this is a nasty edge case. This is not just my personal opinion, but Google's ErrorProne suite bans this usage because of it.. Moreover, they tried to fix the behavior in JDK bug 6559590, but couldn't, because too much broken code depends on old behavior.
The old Android documentation in java.util.regex.Pattern used to be very explicit about it:
For n == 0, the result is as for n < 0, except trailing empty strings will not be returned. (Note that the case where the input is itself an empty string is special, as described above, and the limit parameter does not apply there.)
Unfortunately, the OpenJDK docs are very unclear, and should be improved in my opinion.
So, I advise you to always pass n == -1 as the second parameter (this will skip step two above), unless you specifically know what you want to achieve / you are sure that the empty string is not something that your program would get as an input.
If you are already using Guava in your project, you can try the Splitter (documentation) class. It has a very rich API, and makes your code very easy to understand.
Splitter.on("").omitEmptyStrings().split(",") // correctly empty array
Splitter.on(".").split(".a.b.c.") // "", "a", "b", "c", ""
Splitter.on(",").omitEmptyStrings().split("a,,b,,c") // "a", "b", "c"
Splitter.on(CharMatcher.anyOf(",.")).split("a,b.c") // "a", "b", "c"
Splitter.onPattern("=>?").split("a=b=>c") // "a", "b", "c"
Splitter.on(",").limit(2).split("a,b,c") // "a", "b,c"
"".split (",", n) generates a one element array for n in (-1, 0, 1) with Oracle JDK 8. Would be nice to get a list of non-empty tokens only -- guess a full regex may be necessary (something like "[^,\\s]+[^,]*[^,\\s]*").For the same reason that
",test" split ','
and
",test," split ','
will return an array of size 2. Everything before the first match is returned as the first element.
String's split if you need more information."".split("wtf").length returns 0. Only in JS it's 1. :/"," split "," returns an array of 0 ?"a".split(",") -> "a"
therefore
"".split(",") -> ""
",".split(",") returns empty array.In all programming languages I know a blank string is still a valid String. So doing a split using any delimiter will always return a single element array where that element is the blank String. If it was a null (not blank) String then that would be a different issue.
This split behavior is inherited from Java, for better or worse...
Scala does not override the definition from the String primitive.
Note, that you can use the limit argument to modify the behavior:
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
i.e. you can set the limit=-1 to get the behavior of (all?) other languages:
@ ",a,,b,,".split(",")
res1: Array[String] = Array("", "a", "", "b")
@ ",a,,b,,".split(",", -1) // limit=-1
res2: Array[String] = Array("", "a", "", "b", "", "")
It's seems to be well-known the Java behavior is quite confusing but:
The behavior above can be observed from at least Java 5 to Java 8.
There was an attempt to change the behavior to return an empty array when splitting an empty string in JDK-6559590. However, it was soon reverted in JDK-8028321 when it causes regression in various places. The change never makes it into the initial Java 8 release.
Note: The split method wasn't in Java from the beginning (it's not in 1.0.2) but actually is there from at least 1.4 (e.g. see JSR51 circa 2002). I am still investigating...
What's unclear is why Java chose this in the first place (my suspicion is that it was originally an oversight/bug in an "edge case"), but now irrevocably baked into the language and so it remains.
"".split(",") still returns a single element array like [""].
String.split(s)for exactly this reason. I have written more in my answer below.