views:

45

answers:

2

Hi, The following Groovy snippet produces weird results to me :

def s = "123456"
assert s.split("").size() == s.size()

Results in :

Assertion failed: 

assert s.split("").size() == s.size()
       | |         |      |  | |
       | |         7      |  | 6
       | |                |  123456
       | |                false
       | [, 1, 2, 3, 4, 5, 6]
       123456

Is there something I've missed on the split() method behaviour or is this a real bug ?

+4  A: 

Why are you using split()? Use toCharArray():

assert s.toCharArray().length == s.size()
Burt Beckwith
+3  A: 

I'm not familiar with Groovy, but I'm going to go out on a limb here, and guess that String.split() works the same way as it does in Java. This means that the argument you're passing to split() is a String which represents a regex - in this case, the empty string. As per the Java docs:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

This means that your call split("") (e.g. "split on the empty string") is equivalent to split("", 0). Every (non-empty) string starts and ends with the empty string. So, your split("") call is in fact giving you [, 1, 2, 3, 4, 5, 6, ], less the trailing empty strings, hence, [, 1, 2, 3, 4, 5, 6].

Matt Ball
Thanks a lot for the explanation. That's indeed what I was missing here.
Philippe