Close

Groovy - String Quotes

[Updated: Dec 3, 2018, Created: Nov 8, 2018]

Double Quotes

They are used just like Java double quotes, except that we can also substitute variables inside double quotes (string Interpolation):

src/DoubleQuotesExample.groovy

def b = true
def date = java.time.LocalDate.now()
def s = "${b}, here's the date: ${date} "
println s
println s.getClass()

Output

true, here's the date: 2018-11-08 
class org.codehaus.groovy.runtime.GStringImpl
Double quoted strings are plain java.lang.String if there's no interpolated expression, but are groovy.lang.GString instances if interpolation is present.

In addition to ${} placeholders, we can also use just $ sign prefixing a dotted expression:

src/DoubleQuotesExample2.groovy

String name = "Edward Campbell"
println "Employee's bytes: $name.bytes";

Output

Employee's bytes: [69, 100, 119, 97, 114, 100, 32, 67, 97, 109, 112, 98, 101, 108, 108]

Inside double quotes any Groovy expression is valid within ${...}:

src/DoubleQuotesExample3.groovy

def num = 10
println "result = ${2 * 5 * num}"

Output

result = 100

To escape $ or ${} we can use a \ before them:

src/DoubleQuotesExample4.groovy

def num = 10
println "result = \${num}"

Output

result = ${num}

Single quotes

Single quotes for String are allowed but they can't do interpolation:

src/SingleQuotesExample.groovy

def b = true
def date = java.time.LocalDate.now()
def s = '${b}, here\'s the date: ${date} '
println s + 'done!'

Output

${b}, here's the date: ${date} done!

Inside the single quoted string, a single quote can be escaped as \' .

Declaring and initializing char variable

As seen above single quotes can be used for string. So when it comes to initializing a variable of type 'char' we cannot use single quotes along with with def or with no type (check out this tutorial on types). In that case we must:

  • Explicitly specify its type (char or Character):
    char c = 'c'
    
  • Do type casting:
    def c = (char) 'c'			
  • Use as operator (this is a shortcut of org.codehaus.groovy.runtime.StringGroovyMethods#asType() method which provides 'dynamic' type conversion):
    def c = 'c' as char
    

src/SingleQuoteAndCharVar.groovy

def c1 = 'c'//cannot declare a char this way in Groovy
println c1.getClass().getName()

char c2 = 'c'//must specify the type
println c2.getClass().getName()

def c3 = (char) 'c'//or must cast to char
println c3.getClass().getName()

def c4 = 'c' as char//or must use 'as' operator
println c4.getClass().getName()

def c5 = 'my string' as char//in this case first char is used
println c5.getClass().getName()
println c5


Output

java.lang.String
java.lang.Character
java.lang.Character
java.lang.Character
java.lang.Character
m

Triple Single quotes

Triple quotes are used as triplets of single quotes.
They can be used for multiline string:

src/TripleQuotesExample.groovy

def str='''Never underestimate
the power of stupid people in 
large groups.'''
println str

Output

Never underestimate
the power of stupid people in
large groups.

They do not support interpolation:

src/TripleQuotesExample2.groovy

def name = 'Mike'
println '''The 
name is ${name}'''

Output

The 
name is ${name}

To avoid line breaks we can use \ at the end of the line (just before the line-break)

src/TripleQuotesExample3.groovy

def str='''Never underestimate \
the power of stupid people in \
large groups.'''
println str

Output

Never underestimate the power of stupid people in large groups.

Triple double quotes

Triple double quoted strings behave like double quoted strings, with the addition that they are multiline.

src/TripleDoubleQuotesExample.groovy

def x = 'stupid'
def str="""Never underestimate
the power of ${x} people in \
large groups."""
println str

Output

Never underestimate
the power of stupid people in large groups.

Slashy string

Slashy strings use / as delimiters. They are useful when using regular expression as we don't have to escape backslashes:

src/SlashyStringExample.groovy

def pattern = /\\data\\.*/
def result = "d:\\example\\data\\files".replaceAll(pattern, "")
println result

Output

d:\example

We only need to escape forward slashes by using \

src/SlashyStringExample2.groovy

def pattern = /\/data\/.*/
def result = "http://example.com/data/files".replaceAll(pattern, "")
println result

Output

http://example.com

Slashy strings are multiline and we can also avoid line breaks by using \ at the end a line:

src/SlashyStringExample3.groovy

def s = /1
2\a
3\
4/

println s

Output

1
2\a
34

Slashy strings also support interpolation:

src/SlashyStringExample4.groovy

def x = "Mike"
def s = /The employee name is $x/
println s

Output

The employee name is Mike

Dollar slashy string

They are surrounded between /$ and $/.
They are multiline. A backslash (\) can be used to avoid line breaks.
They support interpolation.
The escape char is $.

src/DollarSlashyQuotesExample.groovy

def x = 5
def s = $/price/cost
is \
$x i.e. $$$x.
And this string used $$/...$/$ quotes
/$
println s

Output

price/cost
is 5 i.e. $5.
And this string used $/.../$ quotes

Example Project

Dependencies and Technologies Used:

  • Groovy 2.5.3
  • JDK 11
Groovy - String Quotes Select All Download
  • groovy-string-quotes
    • src
      • DoubleQuotesExample.groovy

    See Also