Add double quotes in a string in groovy script

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Add double quotes in a string in groovy script

zedfauji
How to add double quotes (") in a string in groovy script.
Suppose
var1 = "a String"

I need output  "a string" and store it into another variable as a string

is it gonna be  def var2 = '\"' + $var1 + '\"' ?
-=Zed=- a.k.a -=Fauji=- More quieter you are , more you can hear
Reply | Threaded
Open this post in threaded view
|

Re: Add double quotes in a string in groovy script

sodul
That's a groovy question.

I recommend that you install groovy and use groovysh to experiment with such things:

$ groovysh
Groovy Shell (2.1.5, JVM: 1.7.0_21)
Type 'help' or '\h' for help.
-----------------------------------------------------------------------------------------------------------------------
groovy:000> var='foo'
===> foo
groovy:000> var2 = "\"${var}\""
===> "foo"
groovy:000> var
===> foo
groovy:000> var2
===> "foo"

http://groovy.codehaus.org/Beginners+Tutorial
Reply | Threaded
Open this post in threaded view
|

Re: Add double quotes in a string in groovy script

frenchyan
Administrator
In reply to this post by zedfauji
groovy allows single and double quotes...

so you can simply use: var1 = '"a String "'

But only " (double quotes) are interpolated =>

def a = 3

var1 = "xxx${a}yyy" // xxx3yyy
var2 = 'xxx${a}yyy' // xxx$[a}yyy

so if you need interpolation you can always use the \" notation (which is java notation).

In general (99% if the cases), groovy is java... so whatever syntax works in java should work in groovy. groovy has more syntax, but if you know java then use java syntax

Yan