Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if values in List is part of String

Tags:

string

list

scala

I have a string like this:

val a = "some random test message" 

I have a list like this:

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

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 ?

( 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.)

like image 512
Sibi Avatar asked Apr 16 '13 20:04

Sibi


People also ask

How do you check if something in a list is a string?

Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1.

How do you check if a part of a string is in a list Python?

Use any() function to check if a list contains a substring in Python. The any(iterable) with iterable as a for-loop that checks if any element in the list contains the substring and returns the Boolean value.

How do you check if a string has a word from a list in Python?

The in operator in Python is basically used to check for data structure membership. It returns either False or True. In Python, we may use the in operator on the superstring to see if a string has a substring. This operator is the best way for using the __contains__ method on an object.


1 Answers

Something like this?

keys.exists(a.contains(_))  

Or even more idiomatically

keys.exists(a.contains) 
like image 139
rarry Avatar answered Sep 23 '22 14:09

rarry