Abhilasha
Abhilasha

Reputation: 21

How to replace a String from Arraylist without iteration In Grails?

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

Answers (2)

cfrick
cfrick

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

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

Related Questions