检查List中的值是否为String的一部分 [英] Checking if values in List is part of String

查看:54
本文介绍了检查List中的值是否为String的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的字符串:

I have a string like this:

val a = "some random test message"

我有一个这样的列表:

val keys = List("hi","random","test")

现在,我要检查字符串a是否包含keys中的任何值.我们如何使用Scala的内置库函数来做到这一点?

Now, I want to check whether the string a contains any values from keys. How can we do this using the in built library functions of Scala ?

(我知道将a拆分为List然后对keys list进行检查然后找到解决方案的方法.但是我正在寻找一种使用标准库函数更简单地解决它的方法.)

( I know the way of splitting a to List and then do a check with keys list and then find the solution. But I'm looking a way of solving it more simply using standard library functions.)

推荐答案

像这样吗?

keys.exists(a.contains(_)) 

或更惯用

keys.exists(a.contains)

这篇关于检查List中的值是否为String的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆