Saturday, December 26, 2009

Groovy Strings - Different ways of creating them

There are different ways to create strings in Groovy - Using single quotes, double quotes, three single quotes, three double quotes and forward slashes. All of them have a purpose. I'll explain the different flavors and when you need to use which one.

1) Single quotes:

Simple:

lst = 'This is my blog post'
println lst
println lst.getClass().name

This is the simplest way to create a string. Here classname returns java.lang.String. The output of this is:

This is my blog post
java.lang.String

Variable Interpolation:

Variable interpolation does not work with single quotes.

postName = "Groovy Strings"
lst = 'This is my blog post $postName'
println lst
println lst.getClass().name

The output of above is:

This is my blog post $postName
java.lang.String

2) Double quotes

Simple:

lst = "This is my blog post"
println lst
println lst.getClass().name

You can also create string using double quotes. Here also class name is java.lang.String. And this behaves same like single quotes. Output is:

This is my blog post
java.lang.String

Variable Interpolation:

But, double quotes also gives us variable interpolation, when needed.

postName = "Groovy Strings"
lst = "This is my blog post $postName"
println lst
println lst.getClass().name

The output of above is shown below. You can see that variable interpolation worked. Also, in this case, since variable interpolation is used, groovy is smart enough to create GStringImpl instead of regular String.

This is my blog post Groovy Strings
org.codehaus.groovy.runtime.GStringImpl

3) Three Single Quotes

Simple:

lst = '''This is my blog post.
I love it.'''
println lst
println lst.getClass().name

This is mainly used to support multi-line Strings. We don't have to use the + operator or the append() method from StringBuffer or StringBuilder anymore. You can see that Strings created using three single quotes is also regular java.lang.String like the one created with single quotes. Output is shown below:

This is my blog post.
I love it.
java.lang.String

Variable interpolation:

Just like single quotes, three single quotes does not support variable interpolation.

postName = "Groovy Strings"
lst = '''This is my blog post.
$postName'''
println lst
println lst.getClass().name

The output of above is:

This is my blog post.
$postName
java.lang.String

4) Three double quotes:

This is similar to three single quotes and is used for supporting multi-line strings. The only difference is, this supports variable interpolation just like single double quotes does.

Simple:

lst = """This is my blog post.
I love it."""
println lst
println lst.getClass().name

Output is shown below. You can see that the class name is regular String.

This is my blog post.
I love it.
java.lang.String

Variable interpolation:

postName = "Groovy Strings"
lst = """This is my blog post.
$postName"""
println lst
println lst.getClass().name

Output is shown below. You can see that class name is GStringImpl.

This is my blog post.
Groovy Strings
org.codehaus.groovy.runtime.GStringImpl

Both three single quotes and three double quotes behave similar to their single quote counterparts except that they support multi-line strings.

Forward Slashes:

Forward slash behaves very similar to single double quotes. It supports variable interpolation. It does not support multi-line strings. One good thing with forward slash string is it does not need any escape characters. It is used to create regular expressions also.

Simple:

lst = /This is my blog post./
println lst
println lst.getClass().name

Output is:

This is my blog post.
java.lang.String

Variable interpolation:

postName = "Groovy Strings"
lst = /This is my blog post - $postName/
println lst
println lst.getClass().name

Output is:

This is my blog post - Groovy Strings
org.codehaus.groovy.runtime.GStringImpl

Escape characters:

pathName = /c:\develop\grooy\myprojectdir/
println pathName

If you use double quotes or single quotes, then you need to escape the slashes i.e pathName = "C:\Develop" will give compile error. It needs to be defined as pathName = "C:\\Develop". Forward slash strings prevent the hassle of providing escape characters.

The output of above is:

c:\develop\grooy\myprojectdir

Regular Expression:

Using forward slashes, also creates a RegEx. For example, /(B/b)log/ creates a RegEx.

lst = "This is my blog post."
pattern = /(B|b)log/
if (lst =~ pattern) {
println 'matching'
} else {
println 'not matching'
}

The output of above is:

matching

=~ returns a matcher and groovy evaluates that to true if matcher has at least one match. Since the matcher had a match in 'blog', the if statement evaluated to true and printed 'matching'.

Conclusion:

We saw several different ways to create Strings in Groovy. This might be confusing for beginners. In the beginning, you might be better off using just double quotes and three double quotes since it supports what single quotes can support and also does variable interpolation. Also, Groovy is smart enough to create the regular java.lang.String if double quotes string does not use variable interpolation.

No comments: