我正在寻找一个单行解决方案来检查字符串是否包含列表中的任何项目。
例子:
字符串:"This is A and B and C"
预期输出:
如果列表=["A"B]然后true
如果列表=["C","D"然后true
如果列表=["D","E"然后false
我认为Java中的Stream()之类的东西可以解决这个问题,但我不确定是否在Scala中使用它。
您可以使用exists
。
val str = "This is A and B and C"
val xs = List("A","C")
val res = xs.exists(s => str.contains(s))
println(res) // True