MissingMethodException没有方法签名:java.lang.String.collectReplacements()

时间:2013-12-05 16:02:01

标签: string groovy

我需要在groovy中编写一些代码来替换字符串

中的非数字字符

我根据网上的内容写了这样的内容

def sx = "00OOoo00"

def replacement = {
    if (it == 'O'){
        '0'
    } else if (it == 'o') {
        '0'
    } else {
        null
    }
}   

sx.collectReplacements(replacement)     

println sx

但编译器抛出错误

groovy.lang.MissingMethodException: No signature of method: java.lang.String.collectReplacements() is applicable for argument types: (ConsoleScript23$_run_closure1) values: [ConsoleScript23$_run_closure1@3bd2ab63]

提前感谢您的回复

1 个答案:

答案 0 :(得分:1)

这不是很常规,但在groovy中你总是可以使用java功能:

sx = sx.replaceAll("o", "0").replaceAll("O", "0")

对于这个简单的任务,你不需要关闭imho