assign “it” in each iteration (groovy)

后端 未结 5 1948
粉色の甜心
粉色の甜心 2021-02-20 03:32

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 th

5条回答
  •  走了就别回头了
    2021-02-20 04:12

    You could also use the spread operator:

    def list = [" foo", "bar ", " groovy "]
    list = list*.trim()
    assert "foo" == list[0]
    assert "bar" == list[1]
    assert "groovy" == list[2]
    

提交回复
热议问题