Multiline Strings

When we want to create a multiline string in Java, we have to use code like str += ..., concatenated multiple lines using the + operator, or multiple calls to the append method of StringBuilder or StringBuffer.

We’d have to use a lot of escape characters, and that usually is followed by a complaint that “there’s gotta be a better way to do that.” In Groovy there is. We can define a multiline literal by enclosing the string within three single quotes (”’…”’)—that’s Groovy’s support of here documents, or heredocs:

WorkingWithStrings/MultilineStrings.groovy
 
memo = ​'''Several of you raised concerns about long meetings.
 
To discuss this, we will be holding a 3 hour meeting starting
 
at 9AM tomorrow. All getting this memo ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.