Java Reference
In-Depth Information
Groovy also has multiline strings, with either single or double quotes. The difference again
is whether or not parameter replacement is done:
def picard = '''
(to the tune of Let It Snow)
Oh the vacuum outside is endless
Unforgiving, cold, and friendless
But still we must boldly go
Make it so, make it so, make it so!
'''
def quote = """
There are ${Integer. toBinaryString (2)} kinds of people in the world:
Those who know binary, and those who don't
"""
assert quote == '''
There are 10 kinds of people in the world:
Those who know binary, and those who don't
'''
There's one final kind of string, used for regular expressions. Java has had regular-expres-
sion capabilities since version 1.4, but most developers either aren't aware of them or avoid
them. [ 6 ] One particularly annoying part of regular expressions in Java is that the backslash
character, \ , is used as an escape character, but if you want to use it in a regular expression,
you have to backslash the backslash. This leads to annoying expressions where you have to
double-backslash the backslashes, making the resulting expressions almost unreadable.
6 Perl programmers love regular expressions. Ruby developers are fond of them, but reasonable about it. Java deve-
lopers take one look at the JavaDocs for the java.util.regex.Pattern class and recoil in horror.
Groovy provides what's called the slashy syntax. If you surround an expression with for-
ward slashes, it's assumed to be a regular expression, and you don't have to double-back-
slash anymore.
Strings
Groovy uses single quotes for regular strings, double quotes for parameterized strings, and
forward slashes for regular expressions.
 
Search WWH ::




Custom Search