list - 使用 List-entrys Groovy 进行模式匹配的优雅方式

标签 list groovy

解决有文本的情况的常用方法是什么

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/

相关文章:

java - 从 Java 向 Groovy 对象添加属性

java - Jmeter:groovy 脚本中 jmeter 对象的包装器

list - 子列表操作

c++ - 添加两个字符列表

python - Python中两个列表的有序交集

java - IntelliJ IDEA 无法运行 Grails 2.3.0 项目

java - 如何在 Groovy 代码中传递带有空格的字符串作为 JMeter 中的参数?

c# - 将数据集转换为列表

c# - 如何用许多字符串值初始化字符串列表 (List<string>)

unit-testing - Groovy Gradle Sonar-runner 单元测试和代码覆盖率