{"id":9994,"date":"2013-04-02T14:02:21","date_gmt":"2013-04-02T08:32:21","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9994"},"modified":"2016-12-19T15:16:42","modified_gmt":"2016-12-19T09:46:42","slug":"groovy-ways-to-represent-string","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-ways-to-represent-string\/","title":{"rendered":"Groovy: Ways to represent String"},"content":{"rendered":"<ul>\n<li>\nCompared to Java we have a lot of\u00a0 ways to represent strings : GString, single quote, double quotes, slashy and dollar slashy.\n<\/li>\n<\/ul>\n<p>&nbsp;<br \/>\n<strong>1.GString<\/strong>: A GString is just like a normal String, except that it evaluates expression that are embedded with in string in the form ${..}.<br \/>\n[java]<br \/>\nString demo=&amp;amp;amp;quot;Brother&amp;amp;amp;quot;<br \/>\nString doubleQuotes= &amp;amp;amp;quot;Hello ${demo}&amp;amp;amp;quot;<br \/>\nprintln doubleQuotes   \/\/Output:Hello Brother<\/p>\n<p>String slashy= \/Hello ${demo}\/<br \/>\nprintln slashy   \/\/Output:Hello Brother<\/p>\n<p>String dollarSlashy= $\/Hello ${demo}\/$<br \/>\nprintln dollarSlashy   \/\/Output:Hello Brother<br \/>\n[\/java]<br \/>\n<strong>2.Single Quote<\/strong>:Groovy Treats a String created using single quotes as a pure literal.<br \/>\n[java]<br \/>\nString demo=&amp;amp;amp;quot;World&amp;amp;amp;quot;<br \/>\nprintln &#8216;Hello &amp;amp;amp;quot;${demo}&amp;amp;amp;quot;&#8217;   \/\/Output: Hello &amp;amp;amp;quot;${demo}&amp;amp;amp;quot;<\/p>\n<p>\/\/To expand an expression Groovy uses double quotes, slashy and dollar slashy.<\/p>\n<p>String multiLine=&#8221;&#8217;With three  Single Quotes<br \/>\nIn Groovy we can write<br \/>\nMulti-Line-String&#8221;&#8217;<br \/>\n[\/java]<br \/>\n<strong>3.Double Quotes<\/strong>:These are often used to define String expression.<br \/>\n[java]<br \/>\nint value=12<br \/>\nprintln &amp;amp;amp;quot;He bought ${value} egg &amp;amp;amp;quot;   \/\/Output: He bought 12 egg<\/p>\n<p>println &amp;amp;amp;quot;He paid \\$${value} for that&amp;amp;amp;quot;   \/\/Output: He paid $12 for that<\/p>\n<p>\/\/I have used the escape  character to print $ symbol since groovy uses that for embedding expressions.you don&#8217;t have to escape $ if you use slashes, as you&#8217;ll see soon.<\/p>\n<p>String multiLine=&amp;amp;amp;quot;&amp;amp;amp;quot;&amp;amp;amp;quot;With three  Double Quotes<br \/>\nIn Groovy we can write<br \/>\nMulti-Line-String&amp;amp;amp;quot;&amp;amp;amp;quot;&amp;amp;amp;quot;<br \/>\n[\/java]<br \/>\n<strong>4.Slashy:<\/strong>These are often used for regular expression.<br \/>\n[java]<\/p>\n<p>int value=12<br \/>\nString demo=\/he paid $${value} for that\/<br \/>\nprintln demo   \/\/Output: he paid $12 for that<\/p>\n<p>\/\/Look here we don&#8217;t have to escape $ symbol<\/p>\n<p>String multiLine=\/With slashy string<br \/>\nIn Groovy we can write<br \/>\nMulti-Line-String\/<\/p>\n<p>[\/java]<br \/>\n<strong>5.Dollar Slashy:<\/strong>It is almost same as slashy string\u00a0but with slightly different escaping rules .The biggest advantage of this is that we don&#8217;t even have to escape slash(&#8216;\/&#8217;) which results in a much cleaner regular expressions but you can use &#8216;$$&#8217; to escape a &#8216;$&#8217; or &#8216;$\/&#8217; to escape a slash if needed.<br \/>\n[java]<\/p>\n<p>int value=12<br \/>\nString demo=$\/he paid $ ${value} for one orange\/$<br \/>\nprintln demo   \/\/ Output :he paid $ 12 for one orange<\/p>\n<p>String demo1=$\/he paid $$ ${value} for one orange\/$<br \/>\nprintln demo1   \/\/ Output: he paid $ 12 for one orange<\/p>\n<p>String demo2=$\/Here evaluation will not take place $${value}, as we have escaped $ with $ \/$<br \/>\nprintln demo2<br \/>\n\/\/ Output: Here evaluation will not take place ${value}, as we have escaped $ with $<\/p>\n<p>String demo3=$\/what would you like to have tea\/coffee\/$<br \/>\nprintln demo3   \/\/Output:what would you like to have tea\/coffee<\/p>\n<p>String demo4=$\/what would you like to have tea$\/coffee\/$<br \/>\nprintln demo4   \/\/Output:what would you like to have tea\/coffee<\/p>\n<p>\/\/you can see that we can have a much cleaner Regex using dollar-slashy string.<\/p>\n<p>String multiLine=$\/With dollar-slashy string<br \/>\nIn Groovy we can write<br \/>\nMulti-Line-String\/$<br \/>\n[\/java]<br \/>\nI hope it helps, feel free to ask if you have any queries<\/p>\n<p>Cheers!!!<br \/>\nVivek Garg<br \/>\nvivek.garg@intelligrape.com<br \/>\n<a href=\"http:\/\/www.tothenew.com\" title=\"www.intelligrape.com\" target=\"_blank\">www.intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Compared to Java we have a lot of\u00a0 ways to represent strings : GString, single quote, double quotes, slashy and dollar slashy. &nbsp; 1.GString: A GString is just like a normal String, except that it evaluates expression that are embedded with in string in the form ${..}. [java] String demo=&amp;amp;amp;quot;Brother&amp;amp;amp;quot; String doubleQuotes= &amp;amp;amp;quot;Hello ${demo}&amp;amp;amp;quot; println [&hellip;]<\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":27},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9994"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9994"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9994\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}