Search code examples
groovygdk

Bug in the Groovy 1.8.0 String class .tokenize(String) method?


Messing around with Groovy for a work project I ran into this oddity:

def string = "__RNDHPD(70.2300000..70.2310000)"
def params = []
string.find(/(?<=\().*?(?=\))/).tokenize("..").each { params << it.trim() }
// should yield [70.2300000,70.2310000] but instead results in [70, 2300000, 70, 2310000]

Using an alternative token works fine though. I don't think I am doing anything wrong, perhaps someone could shed some light on whether this my problem or something I should report to the Groovy devs.


Solution

  • It's not a bug, the documentation is just very poor. The tokenize method is just a wrapper around StringTokenizer, so the string you are passing to it is actually a list of delimiter characters. Try the split method instead.

    def string = "__RNDHPD(70.2300000..70.2310000)"
    def params = []
    string.find(/(?<=\().*?(?=\))/).split(/\.\./).each { params << it.trim() }
    
    assert params == ['70.2300000','70.2310000']