17. Strings

There are two main ways in which you can declare a string in Groovy: single or double quotes

The String section
Method Usage
Single quotes ('...') These are fixed strings and tell Groovy that the string is as we’ve written it (e.g. def pet = 'dog').
Double quotes ("...") These are called GStrings and let us interpolate (insert) variables into our string. (e.g. def petDescription = "My pet is a $pet")
Three single quotes ('''...''') A multi-line fixed string
Three double quotes ("""...""") A multi-line GString

Here’s a quick example of a fixed string and a GString in action:

Fixed strings and GStrings
def pet = 'dog'
def petDescription = "My pet is a $pet"
println petDescription

Escape sequences

Strings can contain escape sequences, allowing you to use non-printable characters in your text.

Escape sequences
Sequence Character
\n line feed
\f form feed
\r carriage return
\t horizontal tab
\’ single quote
\” double quote
\\ backslash

The line feed (\n) is often used to move to a new line:

The line feed
print 'Hi \n there\n'

You’ll notice the use of print in the example above - the final \n performs the same as println and moves to a new line.

The form feed (\f) and carriage return (\r) aren’t often used. Form feed indicates a new page and carriage return goes back to the start of the line.

The horizontal tab (\t) is essentially the same as the tab key on your keyboard. It’s useful for formatting things like tables of information:

Sequences
println 'name\tage\tcolour'
println 'Sam\t12\tblue'
println 'Alice\t8\tgreen'

If you wish to use a quote within your string that matches the quote type you’re using to surround your string then you need to escape the internal quote using the \ character. In the code below you can see the quotes being escaped (\' and \"):

Escape, escape!
println 'That\'s mine'
println "I said \"NO!\""

As the backslash (\) is used to escape characters, it needs an escape of its own. In order to use a backslash in a string you need to double it up (\\) as in the example below:

Backslashing
println 'c:\\documents\\report.doc'

GStrings

In order to have Groovy interpolate the value of a variable we use the $ symbol in front of the variable name - as you can see with $pet below:

GStrings
def pet = 'dog'
println "I own a $pet"

This can be handy if you have a number of variables that you’d like to use in a string:

Interpolating strings
def name = 'Sally'
def hobby = 'surfing'
println "Did you know that $name likes $hobby?"

GStrings also let you interpolate more complicated expressions into a string by using ${...}. In the following example we perform a calculation within the GString:

Operation in a GString
println "10 to the power of 6 is ${10**6}"

We can also access information about a variable in the same manner:

Operation in a GString
def word = 'Supercalifragilisticexpialidocious'
println "$word has ${word.length()} letters"

Multiline Strings

The examples given so far use short strings but longer strings would be cumbersome to type using \n all over the place. Instead, Groovy provides multiline strings - the code below declares a multiline fixed string:

A Multiline string
def poem = '''But the man from Snowy River let the pony have his head,
And he swung his stockwhip round and gave a cheer,
And he raced him down the mountain like a torrent down its bed,
While the others stood and watched in very fear.'''

print poem

If you run the code above you’ll see that new lines are used at the correct points in the display but the first line is not quite right. You can modify this slightly and place a backslash (\) at the start of the string - using statement continuation for readability:

Fixing the first line
def poem = '''\
But the man from Snowy River let the pony have his head,
And he swung his stockwhip round and gave a cheer,
And he raced him down the mountain like a torrent down its bed,
While the others stood and watched in very fear.'''

print poem

GStrings can also be defined using the multiline format:

A multiline GString
def animal = 'velociraptor'

println """But the man from Snowy River let the ${animal} have his head,
And he swung his stockwhip round and gave a cheer,
And he raced him down the mountain like a torrent down its bed,
While the others stood and watched in very fear."""

Building Strings

Working with basic strings is fine but if you need to build up a large piece of text throughout a program they can become very inefficient. We’ll look into this in the tutorial on Operators.

Useful Methods

Strings (text) are important aspects to human-based systems so most programming languages provide a number of methods for modifying, search, slicing and dicing strings. Groovy provides a number of helpful methods you can use with strings and we’ll look at just a few of them here:

  • length() : returns the number of characters in a string
  • reverse(): returns the mirrored version of the string
  • toUpperCase() and toLowerCase(): returns the string with all of the characters converted to upper or lower case.
Some String methods
def str = 'Hello, World'
println str.length()
println str.reverse()
println str.toUpperCase()
println str.toLowerCase()

The trim() method returns the string with any leading and trailing whitespace removed:

Trimming a String
def str = '  Hello, World  '
println str.trim()

The substring method returns a subsection of a string and can be used in two possible ways:

  • Provide a start index (e.g. substring(7)) to get the subsection that includes that index (i.e. the 7th character in the string) through to the end of the string
  • Provide a start and an end index (e.g. substring(7, 9)) to get the subsection that includes that start index through to the end index of the string
Substrings
def str = 'Hello, World'
println str.substring(7)
println str.substring(7,9)

A number of methods are provided to help you with basic searching:

  • The indexOf and lastIndexOf methods return the index (location) of the specified character in the string
  • contains, startsWith, and endsWith return true or false if the supplied parameter is located within the string
Basic searching
def str = 'Hello, World'

//These methods return the index of the requested character
println str.indexOf(',')
println str.lastIndexOf('o')

//These methods check if the string contains another string
println str.contains('World')
println str.startsWith('He')
println str.endsWith('rld')

The replace method lets us provide a string that we want to change to a new value:

Replacing text
def str = 'Hello, World'

println str.replace('World', 'Fred')

Lastly, and a favourite of mine, is toURL(). This converts a String to a URL object which, in Groovy has a great text property that lets us load the text of our favourite web page:

println 'http://www.example.com/'.toURL().text