"".split("\\s");...what would you expect the result to be?
If it helps, the contract for
String.split()is that it takes a regular expression (in this case
\smeans "anything that could reasonably be considered whitespace, like spaces, tabs, et cetera"), and returns a
My intuition was that it would return an empty
String. (Author's Note: which would have been better than a null, but even that's not what I got.)
I mean think about it: what does it mean to split the empty string, on any character? The empty string has length 0, so what can you split it into? Two strings of length 0? But then you could do that ad infinitum, which would be silly.
So what Java actually does is return an array with one string: the empty string, or one string of length 0. Which implies that Java thinks the beginning of a string -- represented by the regex
^if you know the string you're regex-ing doesn't contain newlines -- is significant, but the end of a string (
$) is "whitespace". I guess you could make a case for this making sense, but it doesn't match up with my intuition.
What do you think?