1. Single-quoted string(单引号的字符串)
class StrStudy {
static void main(String[] args) {
def str = 'a single-quoted string';
println(str);
}
}
2. String concatenation
All the Groovy strings can be concatenated with the +
operator
意思是:所有Groovy字符串都可以用+操作符连接起来
class StrStudy {
static void main(String[] args) {
def str = 'a' + 'b'
println(str)
}
}
这和很多语法都是一样的.
3. Triple-single-quoted string
三对引号的字符串
class StrStudy {
static void main(String[] args) {
def str = '''a triple-single-quoted string'''
println(str)
}
}
如果是上面的使用方式那么和上面没啥区别,那么三个单引号或者三个双引号的作用在哪呢?换行显示
class StrStudy {
static void main(String[] args) {
def str = '''line one
line two
line three'''
println(str)
}
}
显示:
line one
line two
line three
Process finished with exit code 0
相当于Java中的换行\n. 这点和dart语言一样
字符串包含一个换行字符作为第一个字符。可以通过使用反斜杠转义换行来删除该字符
class StrStudy {
static void main(String[] args) {
def str = '''\
line one
line two
line three
'''
println(str)
}
}
显示:
如果没有/ 显示结果:
对比发现line one上面的一个有空格一个没有空格
4. Escaping special characters
Escape sequence | Character |
---|---|
\t | tabulation |
\b | backspace |
\n | newline |
\r | carriage return |
\f | formfeed |
\\ | backslash |
\' | single quote within a single-quoted string (and optional for triple-single-quoted and double-quoted strings) |
\" | double quote within a double-quoted string (and optional for triple-double-quoted and single-quoted strings) |
这是官网给我们的文档, 基本和Java一样
5. Double-quoted string
这是双引号定义的字符串写法
class StrStudy {
static void main(String[] args) {
def str = "a double-quoted string"
println(str)
}
}
6. String interpolation(字符串表达式)
使用方式 ${}
class StrStudy {
static void main(String[] args) {
def name = 'Guillaume'
def greeting = "Hello ${name}"
println(greeting)
}
}
结果:
Hello Guillaume
这点和dart语法一样
任何Groovy表达式都是有效的,
class StrStudy {
static void main(String[] args) {
def a = 2
def b = 3
def sum = "a + b = ${a + b}"
println(sum)
}
}
打印结果:
a + b = 5
在语句中也可以使用${}表达式
class StrStudy {
static void main(String[] args) {
def str = "${def a = 1; def b = 2; a + b}"
println(str)
}
}
打印结果:
5
表达式在语句中会以最后的表达式结果为输出,前面定义的变量不算
7:Special case of interpolating closure expressions(闭包的表达式)
但是对于闭包表达式有一个特殊的情况和符号。当占位符包含一个箭头${→}时,表达式实际上是一个闭包表达式—您可以将它看作一个闭包,在它前面有一个$前缀
class StrStudy {
static void main(String[] args) {
def str = "1 + 2 == ${-> 3}"
println(str)
}
}
结果:
1 + 2 == 3
上面的是:闭包是一个不接受参数的无参数闭包
还可以在闭包中使用操作符:
class StrStudy {
static void main(String[] args) {
def str = "1 + 2 == ${ w -> w << 3}"
println(str)
}
}
7:Triple-double-quoted string
在三双引号字符串中使用表达式
class StrStudy {
static void main(String[] args) {
def name = 'Groovy'
def str = """
Dear Mr ${name},
You're the winner of the lottery!
Yours sincerly,
Dave
"""
println(str)
}
}
结果:
Dear Mr Groovy,
You're the winner of the lottery!
Yours sincerly,
Dave
8. Slashy string
这是groovy语言独有的字符串定义方式
class StrStudy {
static void main(String[] args) {
def str = /.*foo.*/
println(str)
}
}
使用/字符串/ 二个斜杆的方式定义一个字符串
如果在字符串中有斜杆需要原始的输出出来。需要转义字符
class StrStudy {
static void main(String[] args) {
def str = /The character \/ is a forward slash/
println(str)
}
}
结果:
The character / is a forward slash
也可以使用这种方式定义一个多行字符串
class StrStudy {
static void main(String[] args) {
def str = /one
two
three/
println(str)
}
}
当然这种方式定义字符串也可以使用${}表达式
class StrStudy {
static void main(String[] args) {
def color = 'blue'
def str = /a ${color} car/
println(str)
}
}
9. Dollar slashy string
这是使用另外一种字符串的方式
Dollar slashy strings are multiline GStrings delimited with an opening $/ and and a closing /$
class StrStudy {
static void main(String[] args) {
def name = "Guillaume"
def date = "April, 1st"
def str = $/
Hello $name,
today we're ${date}.
$ dollar sign
$$ escaped dollar sign
\ backslash
/ forward slash
$/ escaped forward slash
$$$/ escaped opening dollar slashy
$/$$ escaped closing dollar slashy
/$
println(str)
}
}
结果:
Hello Guillaume,
today we're April, 1st.
$ dollar sign
$ escaped dollar sign
\ backslash
/ forward slash
/ escaped forward slash
$/ escaped opening dollar slashy
/$ escaped closing dollar slashy
注意上面的转义字符
10. String summary table
String name | String syntax | Interpolated | Multiline | Escape character |
Single-quoted |
|
|
|
|
Triple-single-quoted |
|
|
|
|
Double-quoted |
|
|
|
|
Triple-double-quoted |
|
|
|
|
Slashy |
|
|
|
|
Dollar slashy |
|
|
|
|