Double Quotes
They are used just like Java double quotes, except that we can also substitute variables inside double quotes (string Interpolation):
src/DoubleQuotesExample.groovydef b = true
def date = java.time.LocalDate.now()
def s = "${b}, here's the date: ${date} "
println s
println s.getClass() Outputtrue, here's the date: 2018-11-08 class org.codehaus.groovy.runtime.GStringImpl
Double quoted strings are plain
java.lang.String if there's no interpolated expression, but are
groovy.lang.GString instances if interpolation is present.
In addition to ${} placeholders, we can also use just $ sign prefixing a dotted expression:
src/DoubleQuotesExample2.groovyString name = "Edward Campbell"
println "Employee's bytes: $name.bytes";
OutputEmployee's bytes: [69, 100, 119, 97, 114, 100, 32, 67, 97, 109, 112, 98, 101, 108, 108]
Inside double quotes any Groovy expression is valid within ${...}:
src/DoubleQuotesExample3.groovydef num = 10
println "result = ${2 * 5 * num}"
Outputresult = 100
To escape $ or ${} we can use a \ before them:
src/DoubleQuotesExample4.groovydef num = 10
println "result = \${num}"
Outputresult = ${num}
Single quotes
Single quotes for String are allowed but they can't do interpolation:
src/SingleQuotesExample.groovydef b = true
def date = java.time.LocalDate.now()
def s = '${b}, here\'s the date: ${date} '
println s + 'done!'
Output${b}, here's the date: ${date} done!
Inside the single quoted string, a single quote can be escaped as \' .
Declaring and initializing char variable
As seen above single quotes can be used for string. So when it comes to initializing a variable of type 'char' we cannot use single quotes along with with def or with no type (check out this tutorial on types). In that case we must:
- Explicitly specify its type (char or Character):
char c = 'c'
- Do type casting:
def c = (char) 'c'
- Use as operator (this is a shortcut of
org.codehaus.groovy.runtime.StringGroovyMethods#asType() method which provides 'dynamic' type conversion): def c = 'c' as char
src/SingleQuoteAndCharVar.groovydef c1 = 'c'//cannot declare a char this way in Groovy
println c1.getClass().getName()
char c2 = 'c'//must specify the type
println c2.getClass().getName()
def c3 = (char) 'c'//or must cast to char
println c3.getClass().getName()
def c4 = 'c' as char//or must use 'as' operator
println c4.getClass().getName()
def c5 = 'my string' as char//in this case first char is used
println c5.getClass().getName()
println c5
Outputjava.lang.String java.lang.Character java.lang.Character java.lang.Character java.lang.Character m
Triple Single quotes
Triple quotes are used as triplets of single quotes. They can be used for multiline string:
src/TripleQuotesExample.groovydef str='''Never underestimate
the power of stupid people in
large groups.'''
println str
OutputNever underestimate the power of stupid people in large groups.
They do not support interpolation:
src/TripleQuotesExample2.groovydef name = 'Mike'
println '''The
name is ${name}''' OutputThe name is ${name}
To avoid line breaks we can use \ at the end of the line (just before the line-break)
src/TripleQuotesExample3.groovydef str='''Never underestimate \
the power of stupid people in \
large groups.'''
println str OutputNever underestimate the power of stupid people in large groups.
Triple double quotes
Triple double quoted strings behave like double quoted strings, with the addition that they are multiline.
src/TripleDoubleQuotesExample.groovydef x = 'stupid'
def str="""Never underestimate
the power of ${x} people in \
large groups."""
println str
OutputNever underestimate the power of stupid people in large groups.
Slashy string
Slashy strings use / as delimiters. They are useful when using regular expression as we don't have to escape backslashes:
src/SlashyStringExample.groovydef pattern = /\\data\\.*/
def result = "d:\\example\\data\\files".replaceAll(pattern, "")
println result
Outputd:\example
We only need to escape forward slashes by using \
src/SlashyStringExample2.groovydef pattern = /\/data\/.*/
def result = "http://example.com/data/files".replaceAll(pattern, "")
println result
Outputhttp://example.com
Slashy strings are multiline and we can also avoid line breaks by using \ at the end a line:
src/SlashyStringExample3.groovydef s = /1
2\a
3\
4/
println s Output1 2\a 34
Slashy strings also support interpolation:
src/SlashyStringExample4.groovydef x = "Mike"
def s = /The employee name is $x/
println s OutputThe employee name is Mike
Dollar slashy string
They are surrounded between /$ and $/. They are multiline. A backslash (\) can be used to avoid line breaks. They support interpolation. The escape char is $.
src/DollarSlashyQuotesExample.groovydef x = 5
def s = $/price/cost
is \
$x i.e. $$$x.
And this string used $$/...$/$ quotes
/$
println s
Outputprice/cost is 5 i.e. $5. And this string used $/.../$ quotes
Example ProjectDependencies and Technologies Used: |