Grails Cookbook - A collection of tutorials and examples

Groovy Multiline String

We will show code snippet examples on how to declare Multiline String in Groovy.

Here is a simple example on how to declare in Groovy a Multiline String using triple single quotes:

def multilineString = '''
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
'''
println multilineString

The output will be:

Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.

Which is multiline as we declared. We can also use triple double quotes to define a multiline String in Groovy. Here is an example:

def multilineString = """
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
"""
println multilineString

Incidentally, double quotes denotes a GString. Which means we can put expressions inside it and it will be evaluated. here is an example:
def a = 10
def b = 15
def multilineString = """
The sum of ${a} and ${b}
is ${a+b}
${a} + ${b} = ${a+b} 
"""
println multilineString
The expressions inside ${} are evaluated and the output will be.
The sum of 10 and 15
is 25
10 + 15 = 25