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.)
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.
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.
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.
Something like this?
keys.exists(a.contains(_))
Or even more idiomatically
keys.exists(a.contains)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With