Groovy: Ways to represent String

02 / Apr / 2013 by Vivek Garg 0 comments
  • Compared to Java we have a lot of  ways to represent strings : GString, single quote, double quotes, slashy and dollar slashy.

 
1.GString: A GString is just like a normal String, except that it evaluates expression that are embedded with in string in the form ${..}.

String demo="Brother"
String doubleQuotes= "Hello ${demo}"
println doubleQuotes   //Output:Hello Brother

String slashy= /Hello ${demo}/
println slashy   //Output:Hello Brother

String dollarSlashy= $/Hello ${demo}/$
println dollarSlashy   //Output:Hello Brother

2.Single Quote:Groovy Treats a String created using single quotes as a pure literal.

String demo="World"
println 'Hello "${demo}"'   //Output: Hello "${demo}"

//To expand an expression Groovy uses double quotes, slashy and dollar slashy.

String multiLine='''With three  Single Quotes
In Groovy we can write
Multi-Line-String'''

3.Double Quotes:These are often used to define String expression.

int value=12
println "He bought ${value} egg "   //Output: He bought 12 egg

println "He paid \$${value} for that"   //Output: He paid $12 for that

//I have used the escape  character to print $ symbol since groovy uses that for embedding expressions.you don't have to escape $ if you use slashes, as you'll see soon.

String multiLine="""With three  Double Quotes
In Groovy we can write
Multi-Line-String"""

4.Slashy:These are often used for regular expression.


int value=12
String demo=/he paid $${value} for that/
println demo   //Output: he paid $12 for that

//Look here we don't have to escape $ symbol

String multiLine=/With slashy string
In Groovy we can write
Multi-Line-String/

5.Dollar Slashy:It is almost same as slashy string but with slightly different escaping rules .The biggest advantage of this is that we don’t even have to escape slash(‘/’) which results in a much cleaner regular expressions but you can use ‘$$’ to escape a ‘$’ or ‘$/’ to escape a slash if needed.


int value=12
String demo=$/he paid $ ${value} for one orange/$
println demo   // Output :he paid $ 12 for one orange

String demo1=$/he paid $$ ${value} for one orange/$
println demo1   // Output: he paid $ 12 for one orange

String demo2=$/Here evaluation will not take place $${value}, as we have escaped $ with $ /$
println demo2  
// Output: Here evaluation will not take place ${value}, as we have escaped $ with $

String demo3=$/what would you like to have tea/coffee/$
println demo3   //Output:what would you like to have tea/coffee

String demo4=$/what would you like to have tea$/coffee/$
println demo4   //Output:what would you like to have tea/coffee

//you can see that we can have a much cleaner Regex using dollar-slashy string.

String multiLine=$/With dollar-slashy string
In Groovy we can write
Multi-Line-String/$

I hope it helps, feel free to ask if you have any queries

Cheers!!!
Vivek Garg
vivek.garg@intelligrape.com
www.intelligrape.com
http://www.linkedin.com/pub/vivek-garg/47/b40/16

Tag -

FOUND THIS USEFUL? SHARE IT

Leave a comment -