解决有文本的情况的常用方法是什么
this is an awesome text and nothing else matters
我有一个像这样的列表
[ "nothing","bread","butter","fool" ]
我想检查字符串中的任何单词是否在列表中
到目前为止我的解决方案是:
Boolean check = false
String myString = "this is an awesome text and nothing else matters"
List myList = [ "nothing","bread","butter","fool" ]
def stringlist = myString.tokenize(" ")
stringlist.each{
if( it in myList ){
check = true
}
}
有没有更高效或更优雅的方式来处理这个问题?
我如何处理标点符号?
提前感谢任何建议
最佳答案
我会考虑使用一些收集方法。 Intersect想到了。
def myString = "this is my string example"
def words = ['my', 'list', 'of', 'stuff']
def matches = myString.tokenize(" ").intersect(words)
println matches
def check = (matches.size() > 0)
关于list - 使用 List-entrys Groovy 进行模式匹配的优雅方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22305322/