Hey, i try to trim each string item of an list in groovy
list.each() { it = it.trim(); }
But this only works within the closure, in the list the strings are still " foo", "bar " and " groovy ".
How can i achieve that?
Hey, i try to trim each string item of an list in groovy
list.each() { it = it.trim(); }
But this only works within the closure, in the list the strings are still " foo", "bar " and " groovy ".
How can i achieve that?
You could also use the spread operator:
If you really had to modify the list in place, you could use list.eachWithIndex { item, idx -> list[idx] = item.trim() }.
collect() is way better.
According to the Groovy Quick Start, using
collect
will collect the values returned from the closure.Here's a little example using the Groovy Shell:
@sepp2k i think that works in ruby
and this works in groovy list = list.collect() { it.trim(); }