I would confuse you even more, saying, that you can also use slash /
, dolar-slash $/
and triple-double quotes """
with same result. =)
So, what's the difference:
- Single vs Double quote: Most important difference. Single-quoted is ordinary Java-like string. Double-quoted is a
GString
, and it allows string-interpolation. I.e. you can have expressions embedded in it: println("${40 + 5}")
prints 45, while println('${ 40 + 5}')
will produce ${ 40 + 5}
. This expression can be pretty complex, can reference variables or call methods.
- Triple quote and triple double-quote is the way to make string multiline. You can open it on one line in your code, copy-paste big piece of xml, poem or sql expression in it and don't bother yourself with string concatenation.
- Slashy
/
and dollar-slashy $/
strings are here to help with regular expressions. They have special escape rules for '' and '/' respectfully.
As @tim pointed, there is a good official documentation for that, explaining small differences in escaping rules and containing examples as well.
Most probably you don't need to use multiline/slashy strings very often, as you use them in a very particular scenarios. But when you do they make a huge difference in readability of your code!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…