{"id":8525,"date":"2012-09-25T01:37:31","date_gmt":"2012-09-24T20:07:31","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8525"},"modified":"2012-09-25T02:17:25","modified_gmt":"2012-09-24T20:47:25","slug":"dollar-slashy-strings-in-groovy-ver-1-8","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/dollar-slashy-strings-in-groovy-ver-1-8\/","title":{"rendered":"Dollar-Slashy Strings in Groovy [Ver 1.8+]"},"content":{"rendered":"<p>Hi,<\/p>\n<p>&nbsp;<\/p>\n<p>Groovy took another leap ahead in the methodology of defining Strings by providing us with &#8220;dollar-slashy&#8221; strings which further minimized the need of escaping any special character as compared to simple &#8220;slashy&#8221; Strings.<\/p>\n<p>&nbsp;<\/p>\n<p>In the Groovy ver:1.8+ you can define Strings as follows:<br \/>\n[java]<br \/>\n\/\/A dollar-slashy string<br \/>\nString message = $\/Hi! I am Kushal Likhi!\/$<\/p>\n<p>println message<br \/>\n\/\/Output: Hi! I am Kushal Likhi!<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>In the above example we saw that string is defined by using a dollar-Slash combination. The biggest advantage of this combination is that we don&#8217;t even have to escape slashes(&#8220;\/&#8221;), which <strong>results in a cleaner better code.<\/strong> <\/p>\n<p>&nbsp;<\/p>\n<p>Dollar-slashy strings can be best used with regular expressions as it will enable us to write <strong>much cleaner regular expressions.<\/strong><br \/>\nAn example of regular expression to find the protocol of the URL.<br \/>\n[java]<br \/>\n\/\/Using simple slashy strings<br \/>\nString protocol = &quot;http:\/\/www.intelligrape.com&quot;.find(\/([^\\\/]+):\\\/\\\/\/){it[1]}<br \/>\nassert protocol == &quot;http&quot; \/\/will be true<\/p>\n<p>\/\/Now using dollar slashy Strings we can implement the same functionality by:<br \/>\nString protocol = &quot;http:\/\/www.intelligrape.com&quot;.find($\/([^\/]+):\/\/\/$){it[1]}<br \/>\nassert protocol == &quot;http&quot; \/\/will be true<\/p>\n<p>\/\/You can see that we have a much CLEANER RegEx using dollar slashy strings.<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Hope it helps!<br \/>\n<strong>Kushal Likhi<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, &nbsp; Groovy took another leap ahead in the methodology of defining Strings by providing us with &#8220;dollar-slashy&#8221; strings which further minimized the need of escaping any special character as compared to simple &#8220;slashy&#8221; Strings. &nbsp; In the Groovy ver:1.8+ you can define Strings as follows: [java] \/\/A dollar-slashy string String message = $\/Hi! I [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8525"}],"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\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8525"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8525\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}