【发布时间】:2020-08-16 02:29:08
【问题描述】:
我想要一个 Groovy 函数,如果传入的值为空字符串,它将返回 null,否则返回字符串。这是我能想到的..
def emptyStringNullConverter(a) {
return a?.toString()?.length() == 0 ? null : a
}
但是有没有 Groovier 的方法来做到这一点?
【问题讨论】:
我想要一个 Groovy 函数,如果传入的值为空字符串,它将返回 null,否则返回字符串。这是我能想到的..
def emptyStringNullConverter(a) {
return a?.toString()?.length() == 0 ? null : a
}
但是有没有 Groovier 的方法来做到这一点?
【问题讨论】:
你可以这样做:
def emptyStringNullConverter(a) {
a ?: null
}
【讨论】:
a?.trim() ?: null