Groovy String Interpolation

Groovy is a scripting language for the Java platform. Groovy provides many powerful features such as closures, type inference, runtime and compile time meta-programming and many more. In this article, we are going to have a look at String interpolation.

First of all, for those who are wondering what “interpolation” is, interpolation is the process of replacing placeholders in String literals (excluding single and triple single quoted strings) with its value.

Before jumping into interpolation feature, let’s have a quick look at mutable String objects in Groovy.

Mutable String objects in Groovy:

The concept of mutable and immutable is not new to the developers familiar with Java. In Groovy, immutable String objects are defined using single quotes or triple single quotes, whereas we use double quotes or triple double quotes to create mutable String objects. Triple quotes (single or double) are used to overcome line boundaries. This basically means that you don’t have to split the content when you extend it to a new line.

In a way, we can say that the double quotes or triple double quotes are an indication to the compiler that we want to create a mutable string or Groovy String (also known as GString). But, this does not necessarily mean that you have to use the interpolation feature. You may leave it as a plain Java String.

Understanding interpolation:

It’s quite simple to use interpolation in GString, all you have to do is surround the placeholder with “${}”. Let’s try to understand this with these examples.

def title = ‘Groovy String Interpolation’
def opinion = ‘feature is awesome.’

def about = “This post is about ${title}.”
def myView = “${title} + ${opinion}”

assert about.toString() == ‘This post is about Groovy String Interpolation.’
assert myView.toString() == ‘Groovy String Interpolation feature is awesome.’

What happened here is, when toString() was called on our GString variables `about` and `myView`, the expression was evaluated and replaced with its String representation.

Note: When a GString does not contain an expression, it is treated as a plain String object. Ex:

def fruits = [‘mango’, ‘orange’]

def plainString1 = ‘foo’
def plainString2 = “bar”

def gstringVar = “${fruits[0]} is a seasonal fruit.”

assert plainString1.class.name == ‘java.lang.String’
assert plainString2.class.name == ‘java.lang.String’

assert gstringVar.class.name == ‘org.codehaus.groovy.runtime.GStringImpl’

It is quite an interesting feature, isn’t it?

There is a lot more you can do with your Groovy String objects. Please refer to the official docs to see some of the advanced features of Groovy String interpolation.

About CauseCode: We are a technology company specializing in Healthtech related Web and Mobile application development. We collaborate with passionate companies looking to change health and wellness tech for good. If you are a startup, enterprise or generally interested in digital health, we would love to hear from you! Let's connect at bootstrap@causecode.com
Have you subscribed to our blogs and newsletter? If not, what are you waiting for?  Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

SUBSCRIBE!

Do you want to get articles like these in your inbox?

Email *

Interested groups *
Healthtech
Business
Technical articles

Archives