Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

' vs " " vs ' ' ' in Groovy .When to Use What?

I am getting really confused in Groovy. When to use what for Strings in Groovy ?

1) Single Quotes - ' '
2) Double Quotes - " "
3) Triple Quotes - '''

My code:

 println("Tilak Rox")
 println('Tilak Rox')
 println('''Tilak Rox''')

All tend to produce same results. When to Use What ?

like image 541
Tilak Maddy Avatar asked May 26 '16 15:05

Tilak Maddy


People also ask

What is ?: In Groovy?

Yes, the "?:" operator will return the value to the left, if it is not null. Else, return the value to the right. "Yes, the "?:" operator will return the value to the left, if it is not null." - That is incorrect.

How do you use special characters in Groovy?

String specialCharRegex = "[\\W|_]"; ... term = term. replaceAll(specialCharRegex, "\\\\\$0"); ... String specialCharRegex = "[\\W|_]"; ...

How do you concatenate two variables in Groovy?

Groovy - Concatenation of Strings The concatenation of strings can be done by the simple '+' operator. Parameters − The parameters will be 2 strings as the left and right operand for the + operator.


2 Answers

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:

  1. 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.
  2. 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.
  3. 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!

like image 119
Seagull Avatar answered Oct 17 '22 21:10

Seagull


Single quotes ' are for basic Strings

Double quotes " are for templated Strings ie:

def a = 'tim'
assert "Hi $a" == 'Hi tim'

Triple single quotes ''' are for multi-line basic Strings

Triple double """ quotes are for multi-line templated strings

There's also slashy strings /hello $a/ which are templated

And dollar slashy Strings $/hello $a/$ which are multi-line and templated

They're all documented quite well in the documentation

like image 23
tim_yates Avatar answered Oct 17 '22 20:10

tim_yates