如何使用 replace 函数转换 groovy 中 arraylist 对象的所有元素

How use replace function to convert all elements of an arraylist object in groovy

我正在尝试在我的 dsl groovy 脚本中转换 arrylist 对象的项目:

branches = ['test/1.2.0', 'test1/1.4.0']

我想得到结果:

branches = ['test_1.2.0', 'test1_1.4.0']

我尝试了这些方法:

branches = branches.each {
    def branchName = it
    branchName = branchName.replaceAll('/','_')
}

branches = branches.each {
    it -> it.replaceAll('//','_')
}

但是,我在执行 println 后得到了同样的结果:

我的结果是:

branches = ['test/1.2.0', 'test1/1.4.0']

感谢您的帮助。

您也可以为此使用展开 (*) 运算符,请查看此处的文档:https://docs.groovy-lang.org/latest/html/documentation/:

branches = ['test/1.2.0', 'test1/1.4.0']
branches = branches*.replace('/', '_')
println branches