可以直接用+



assert 'ab' == 'a' + 'b'


 

支持多行



def aMultilineString = '''line one
line two
line three'''


 out:

Fri Jan 03 11:14:58 CST 2020: INFO: line one

line two

line three


如果定义多行字符串时有缩进,可以去掉,下面俩方法就行



import java.lang.String
import java.lang.GString
String#stripIndent()
String#stripMargin()


 

巧合了,字符在行开头,下面两种一样



def aMultilineString1 = '''
line one
line two
line three'''

def aMultilineString2 = '''\n
line one
line two
line three'''

log.info aMultilineString1
log.info aMultilineString2


 Groovy 字符串那点儿事儿_官网

 

 

让字符串里出现 引号 和反斜杠

Groovy 字符串那点儿事儿_Groovy_02

 

 

 

下面这些是用反斜杠也能出现在字符串里

Groovy 字符串那点儿事儿_java_03

 

 

Groovy 字符串那点儿事儿_官网_04

 

 

花括弧里面的



def sum = "The sum of 2 and 3 equals ${2 + 3}"
log.info sum
assert sum == 'The sum of 2 and 3 equals 5'
assert sum.toString() == 'The sum of 2 and 3 equals 5'


 Fri Jan 03 14:07:08 CST 2020: INFO: The sum of 2 and 3 equals 5




def sum = "The sum of 1 and 2 is equal to ${def a = 1; def b = 2; a + b}" 
log.info sum
assert sum == 'The sum of 1 and 2 is equal to 3'
assert sum.toString() == 'The sum of 1 and 2 is equal to 3'


Fri Jan 03 14:11:23 CST 2020: INFO: The sum of 1 and 2 is equal to 3