开发者

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.


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']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜