字符串分割
"abc,def".split(",")
// 如果使用 Pipe(|) 进行分割:
"abc|def".split("\\|")
将字符串作为文本行进行遍历
方法一、使用 eachLine 进行遍历:
def multiline = '''\
Groovy is closely related to Java,
so it is quite easy to make a transition.
'''
multiline.eachLine {
if (it =~ /Groovy/) {
println it // Output: Groovy is closely related to Java,
}
}
方法二、先分割,后遍历:
def multiline = '''\
Groovy is closely related to Java,
so it is quite easy to make a transition.
'''
def lines = multiline.split("\\r?\\n");
for (String line : lines) {
println line
}
判断字符串是否匹配正则表达式
使用 ==~ 操作符:
assert "2009"