title: Groovy 字符串String与GStringdate: 2021-07-31 18:12:44.14

updated: 2021-07-31 22:17:06.049
url: /?p=402
categories: Groovy
tags:

String与GString 定义方式有三种单引号相当于java中的普通字符串 双引号可以将表达式包含在字符串中,又称可扩展字符串 三个连续的单引号 可以用原始格式输出,比如换行回车等特殊字符不需要转义。

String相关API

字符串

在Groovy种有两种字符串类型,普通字符串(java.lang.String)和插值字符串(groovy.lang.GString)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 普通字符串使用单引号
println 'hello world'
// 插值字符串使用双引号
def name = '张三'
println "hello $name"
// 支持三单引号的写法,可以保留文本的换行及缩进格式
def strippedFirstNewline = '''line one
line two
line three
'''
println strippedFirstNewline
// 可以写成下面这种形式,可读性更好
def strippedFirstNewline2 = '''\
line one
line two
line three
'''
println strippedFirstNewline2