简体   繁体   中英

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? I am basically looking for the Ruby equivalent of:

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

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

But if you want to be on the safe side, always put the code inside ${}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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