def names = "lucas,Fred,Mary"
for (i in names.split(',')) {
println '"'+i+'"'
}
另一种方法(但可能很棘手,因为你要处理变量):
public class NamesBinding extends Binding{
@Delegate Binding delegate;
public Object getVariable(String name) {
//when variable declared - return the value, otherwise return the quoted name
return delegate.hasVariable(name) ? delegate.getVariable(name) : "${groovy.json.JsonOutput.toJson(name)}"
}
}
this.setBinding(new NamesBinding(delegate:this.getBinding()))
def names = [lucas,Fred,Mary]
for (i in names) {
println i
}
2条答案
按热度按时间62lalag41#
我猜如果 names 是一个字符串数组
vngu2lb82#
你可以用字符串代替数组
另一种方法(但可能很棘手,因为你要处理变量):