简体   繁体   English

Groovy中的字符串连接似乎不方便

[英]string concatenation in Groovy seems inconvenient

My goal is to write this: 我的目标是写这样的:

println "this should be 3: ($1+2)" //this is invalid groovy, it won't run

Yet this is valid in ruby. 但这在红宝石中有效。 Is there a way I can put statements that will eval inside a string or must I use complete variables names? 有什么方法可以将将eval的语句放在字符串中,还是必须使用完整的变量名? I am basically looking for the Ruby equivalent of: 我基本上是在寻找与Ruby等价的产品:

puts "this shoud be 3: #{1+2}" #this is valid ruby

This is what you need 这就是你所需要的

println "this should be 3: ${1+2}"

If the code being evaluated is a variable name or a GPath expression you can omit the curly braces, eg 如果要评估的代码是变量名或GPath表达式,则可以省略花括号,例如

def foo = "bar"
println "The value is $foo"

But if you want to be on the safe side, always put the code inside ${} 但是,为了安全起见,请始终将代码放在${}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM