Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 25 Next »

Groovy uses both " and ' for strings. Either can be used. Using either type of string allows you to use strings with quotations easily.

The groovy parser supports the notation \uab12 (i.e. a leading backslash and precisely four hex digits after the 'u' ).
This notation can be used in strings or anywhere in the program like the Java parser does.


Strings may be concatenated with "+". For example:

Multi-line strings

Regular strings in Groovy cannot span multiple lines.

As an exception to this rule, a backslash at the end of a line disappears and joins the current line with the next.

If you have a block of text which you wish to use but don't want to have to encode it all (e.g. if its a block of HTML or something) then you can use the """ syntax.

Because of the leading backslash, the string text contains exactly two newlines. There are always represented by the character '\n', regardless of the line-termination conventions of the host system.

String literals

It is possible to use another notation for String literals with the added benefit of not needing additional backslashes to escape special characters. That is especially handy with regular expressions.

For more information, read about Regular Expressions.

Strings are immutable

This can be seen with these two code snips, which you can cut and paste into groovyConsole:

Above both variables are references to the map.

If you do the same thing with Strings, the behavior is different:

Here is the explanation by Guillaume Laforge:

sn and st point at the very same map object in memory in the first example, while in the second snippet, at the end, st points at a  different place in memory where there's the new immutable string.


Strings that are declared inside double-quotes (i.e. either single double-quotes ore tripled double-quotes for multi-line strings) can contain arbitrary expressions inside them as shown above using the ${expression} syntax in a similar way to JSP EL, Velocity and Jexl. Any valid Groovy expression can be enclosed in the ${...} including method calls etc. GStrings are defined the same way as normal Strings would be created in Java.

What actually happens is whenever a string expression contains a ${...} expression then rather than a normal java.lang.String instance, a GString object is created which contains the text and values used inside the String. GString uses lazy evaluation so its not until the toString() method is invoked that the GString is evaluated.

This lazy evaluation is useful for things like logging as it allows the calculation of the string, the calls to toString() on the values and the concatenation of the different strings to be done lazily if at all.

Another use case for GString is GSQL.

where parameters can be passed into SQL statements using this same mechanism which makes for a neat way to integrate Groovy with other languages like SQL. GroovySql then converts the expressions to ? and uses a JDBC PreparedStatement and passes the values in, preserving their types.

If you explicitly want to coerce the GString to a String you can use the toString() method. Groovy can also automatically coerce GStrings into Strings for you.

String and StringBuffer Methods

See: Strings and StringBuffers in Groovy.

  • No labels