Search

Dark theme | Light theme

April 28, 2009

Escaping quotes in Groovy strings

Groovy supports strings enclosed in single or double quotes and between slashes. If we use single quotes we don't have to escape double quotes. And if we use double quotes we don't have to escape single quotes. We can use the slashes if we have a string with both double and single quotes and we don't want to escape them.

def singleQuote = 'Single quote string to "demo" double quotes without backslash escape.'
def doubleQuote = "Double quote string let's us use single quote without backslash escape."
def slashy = /Slashy string let's us use single and "double" quotes without backslash escapes./

println singleQuote
println doubleQuote
println slashy

When we run this Groovy script we get the following output:

Single quote string to "demo" double quotes without backslash escape.
Double quote string let's us use single quote without backslash escape.
Slashy string let's us use single and "double" quotes without backslash escapes.