concept gstring in category groovy

appears as: GStrings, GString, GString
Groovy in Action, Second Edition

This is an excerpt from Manning's book Groovy in Action, Second Edition.

Even trying to demonstrate just a few features of Groovy, you’ve seen other features in the preceding examples—string interpolation with GString, simpler for loops, optional typing, and optional statement terminators and parentheses, just for starters. The features work so well with each other and become second nature so quickly, you hardly notice you’re using them.

Groovy strings come in two flavors: plain strings and GStrings. Plain strings are instances of java.lang.String, and GStrings are instances of groovy.lang.GString. GStrings allow placeholder expressions to be resolved and evaluated at runtime. Many scripting languages have a similar feature, usually called string interpolation, but it’s more primitive than the GString feature of Groovy. Let’s start by looking at each flavor of string and how it appears in code.

3.4.2. Working with GStrings

GStrings are like strings with additional capabilities.[13] They’re literally declared in double quotes. What makes a double-quoted string literally a GString is the appearance of placeholders. Placeholders may appear in a full ${expression} syntax or an abbreviated $reference syntax. See the examples in the following listing.

13 groovy.lang.GString isn’t actually a subclass of java.lang.String, and couldn’t be, because String is final. But GStrings can usually be used as if they were strings—Groovy coerces them into strings when it needs to.

Listing 3.4. Working with GStrings
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage