我想要一个 Groovy 函数,如果传入的值是空字符串,则该函数将返回 null,否则返回该字符串。这就是我能想到的..
def emptyStringNullConverter(a) {
return a?.toString()?.length() == 0 ? null : a
}
但是有没有更好的方法来做到这一点?
最佳答案
你可以这样做:
def emptyStringNullConverter(a) {
a ?: null
}
关于string - 将空字符串转换为 null 的 Groovy 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61545204/