首页 > 解决方案 > 如何使用替换函数在groovy中转换arraylist对象的所有元素

问题描述

我正在尝试在我的 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']

谢谢您的帮助。

标签: groovyjenkins-groovy

解决方案


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

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

推荐阅读