|
||||||||||
|
||||||||||
GroovyUsing Groovy
Language GuideGroovy FeaturesModulesExamplesUseful LinksIDE SupportSupportCommunityDevelopersTools we use
Feeds
|
Groovy uses both " and ' for strings. Either can be used. Using either type of string allows you to use strings with quotations easily.
println "he said 'cheese' once" println 'he said "cheese!" again' The groovy parser supports the notation \uab12 (i.e. a leading backslash and precisely four hex digits after the 'u' ). Multi-line stringsStrings in Groovy can span multiple lines. This makes outputting a large block of text easy.
foo = "hello
there
how are things?"
println(foo) Here-docsIf you have a block of text which you wish to use but don't want to have to encode it all (e.g. if its a block of HTML or something) then you can use here-docs. name = "James" text = <<<FOO hello there ${name} how are you today? FOO assert text != null println(text) GStringsStrings can contain arbitrary expressions inside them as shown above using the ${expression} syntax in a similar way to JSP EL, Velocity and Jexl. Any valid Groovy expression can be enclosed in the ${...} including method calls etc. GStrings are defined the same way as normal Strings would be created in Java. What actually happens is whenever a string expression contains a ${...} expression then rather than a normal java.lang.String instance, a This lazy evaluation is useful for things like logging as it allows the calculation of the string, the calls to toString() on the values and the concatenation of the different strings to be done laziy if at all. Another use case for GString is GroovySql where parameters can be passed into SQL statements using this same mechanism which makes for a neat way to integrate Groovy with other languages like SQL. GroovySql then converts the expressions to ? and uses a JDBC PreparedStatement and passes the values in, preserving their types. If you explicitly want to coerce the GString to a String you can use the toString() method. Though Groovy will automatically coerce GStrings into typesafe String methods for you. |
|||||||||
|