Groovy 字符串那点儿事儿

可以直接用+

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 = '''

line one
line two
line three'''

log.info aMultilineString1
log.info aMultilineString2

 

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

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

花括弧里面的

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

 

知识来源官网

http://docs.groovy-lang.org/docs/latest/html/documentation/#_shebang_line

原文地址:https://www.cnblogs.com/baxianhua/p/12144114.html