groovy - 如何使用替换函数在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']
谢谢您的帮助。
解决方案
您也可以为此使用展开 (*) 运算符,请在此处查看文档:https ://docs.groovy-lang.org/latest/html/documentation/ :
branches = ['test/1.2.0', 'test1/1.4.0']
branches = branches*.replace('/', '_')
println branches
推荐阅读
- html - Bootstrap 4 导航栏切换器位置随定义的高度变化?
- javascript - 从 API 返回的时间戳获取用户年龄
- php - PDOException - 连接失败 - 找不到驱动程序 - PHP5/SQLite/Ubuntu
- python - 使用 tkinter.button.place() 时遇到问题
- javascript - 微调器继续时如何禁止修改数据
- swift - 不断滚动的背景
- javascript - try-catch 应该如何与多个事件处理程序一起使用?
- reactjs - 'action' 没有在 reducer 中定义 - React, Redux
- qt - 如何使用 Qt QWidget 边框的 styleSheet 禁用抗锯齿
- algorithm - Kayak 搜索/数据聚合算法