Groovy has two string types the java java.lang.String
and groovy.lang.GString
, as well as multiple forms of string literals (see syntax and examples).
The main difference between the two types of strings is that GString supports string interpolation.
def str = 'Single quoted string'
assert str instanceof String
def str = "Double quoted string"
assert str instanceof String
def param = 'string'
def str = "Double quoted ${param}"
assert str instanceof GString
assert str == 'Double quoted string'
The parameter is by default resolved eagerly, this means this applies:
def param = 'string'
def str = "Double quoted ${param}"
param = 'another string'
assert str == 'Double quoted string'
In order to load the parameter lazily every time the string is used, this can be done:
def param = 'string'
def str = "Double quoted ${ -> param}"
assert str == 'Double quoted string'
param = 'lazy load'
assert str == 'Double quoted lazy load'
def str = '''multiline
string'''
assert str instanceof String
def str = '''
multiline
string'''
assert str.readLines().size() == 3
def str = '''\
multiline
string'''
assert str.readLines().size() == 2
def param = 'string'
def str = """
multiline
$param
"""
assert str instanceof GString
assert str.readLines().size() == 3
assert str == '''
multiline
string
'''
def str = /
multiline string
no need to escape slash
\n
/
assert str instanceof String
assert str.readLines().size() == 4
assert str.contains('\\n')
def param = 'string'
def str = /
multiline $param
no need to escape slash
\n
/
assert str instanceof GString
assert str.readLines().size() == 4
assert str.contains('\\n')
assert str.contains('string')
def param = 'string'
def str = $/
multiline $param
no need to escape slash
\n
$
$$
/$
assert str instanceof GString
assert str.readLines().size() == 6
assert str.contains('\\n')
assert str.contains('$')