String Groovy字符串变量替换之间的区别

String Groovy字符串变量替换之间的区别,string,groovy,String,Groovy,当查看gstring中变量替换的示例时,我注意到两种不同的语法。这可以在这里看到: 例如: def text = 'Dear "$firstname $lastname",\nSo nice to meet you in <% print city %>.\nSee you in ${month},\n${signed}' def text='亲爱的“$firstname$lastname”,\n很高兴在${month}见到您,\n${signed}' 看起来${variable

当查看gstring中变量替换的示例时,我注意到两种不同的语法。这可以在这里看到:

例如:

def text = 'Dear "$firstname $lastname",\nSo nice to meet you in <% print city %>.\nSee you in ${month},\n${signed}'
def text='亲爱的“$firstname$lastname”,\n很高兴在${month}见到您,\n${signed}'
看起来${variable}在您有表达式时更常用,但是$variable在您只有一个变量时使用,但即使在这里,它们也将$firstname和${month}混合使用。当你只有一个变量而没有一个表达式时,你有理由这样或那样做吗?或者这无关紧要?

这无关紧要

正如您所说,如果您有一个像
“${name.toUpperCase()}”
“${number}th”
“${list[0]}”
这样的表达式,那么它必须位于大括号内,但
“${name}”
“$name”
都是相同的

实际上,只要是简单的属性访问,就可以省略大括号,即:
“Hello$person.username”

可以说,添加大括号可以使字符串模板更易于阅读,但这是个人偏好