Thursday, January 28, 2010

Groovy-Strings-Heredocs Step 16

There are interesting things you can do with Strings in Groovy.

def s1='My name is "Joe"'
def s2="My name is 'Joe'"

Quotes can be used to hold a String. This can save you time from having to escape the string if the string contains single or double quotes.

String s= """This is a multi-line String "You don't have to escape it" """

A Heredoc allows you to store multiline Strings in a single variable. Groovy uses triple quotes to define heredocs. (Three single or double quotes)

if you type a variable like this:

def htmlexample="""
<book id="111">
<title>Groovy Test</title>
</book>"""

println htmlexample

You will get this as a printout:

<book id="111">
<title>Groovy Test</title>
</book>

0 comments: