字符串分割
Groovy - split() - Tutorialspoint
java - Grails: Splitting a string that contains a pipe - Stack Overflow
"abc,def".split(",")
// 如果使用 Pipe(|) 进行分割:
"abc|def".split("\|")
将字符串作为文本行进行遍历
Java String Split Newline - Grails Cookbook
Groovy Goodness: Working with Lines in Strings - Messages from mrhaki
方法一、使用 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" ==~ /d+/ String regex = /^somedata(:somedata)*$/ assert "somedata" ==~ regex
使用正则表达式进行字符串替换
def mphone = "1+555-555-5555" println mphone.replace(/5/, "3") // 1+333-333-3333 println mphone.replaceFirst(/5/, "3") // 1+355-555-5555
字符串追加(printf alternative)
据我们所知,函数 printf 只能为字符串追加空格,以使其达到某个长度。
在 Groovy 中,可以使用 padLeft() 或者 padRight() 方法:
println "123123".padLeft(10, "#") // ####123123
相关文章
「Apache Groovy」- 常用列表(List)操作
参考文献
Simple Groovy replace using regex - Stack Overflow
java - How to check if a String matches a pattern in Groovy - Stack Overflow
Pad a String with Zeros or Spaces in Java | Baeldung
How can I pad a String in Java? - Stack Overflow
最后
以上就是欢呼招牌最近收集整理的关于「Groovy」- String(学习笔记) @20210213的全部内容,更多相关「Groovy」-内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复