Reputation: 21
I want to replace some Words in String without using any loops.
def tagList= []
def tags= []
tags.each { tag ->
def newString = tag.replaceAll("sample", "")
tagList.add(newString)
}
Upvotes: 1
Views: 266
Reputation: 37008
It will be hard modify a list without iterating it (I assume, that this demand was just poorly phrased). each
is usually used for side-effects. If you want to transform a list use collect
.
If you only do one operation on each element, you can use the Spread Operator instead:
def tagList = tags*.replaceAll('sample', '')
Upvotes: 0
Reputation: 51
This is the goodness of groovy you can directly replace string without iterating the List.
please try below code
def tagList= []
tagList = tags.join(',').replaceAll('sample','').split(',')
Upvotes: 2