I've got a long string-variable and want to find out whether it contains one of two substrings.
e.g.
haystack = 'this one is pretty long' needle1 = 'whatever' needle2 = 'pretty'
Now I'd need a disjunction like this which doesn't work in Ruby though:
if haystack.include? needle1 || haystack.include? needle2 puts "needle found within haystack" end
You can use any : a_string = "A string is more than its parts!" matches = ["more", "wholesome", "milk"] if any(x in a_string for x in matches): Similarly to check if all the strings from the list are found, use all instead of any .
Using regular expressions, we can easily check multiple substrings in a single-line statement. We use the findall() method of the re module to get all the matches as a list of strings and pass it to any() method to get the result in True or False.
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.
[needle1, needle2].any? { |needle| haystack.include? needle }
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