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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM