Check If String Contains Any Substring in an Array in Ruby

Check if string contains any substring in an array in Ruby

I think we can divide this question in two:

  1. How to clean undesired data
  2. How to check if cleaned data is valid

The first is well answered above. For the second, I would do the following:

(cleaned_content_types - VALID_CONTENT_TYPES) == 0

The nice thing about this solution is that you can easily create a variable to store the undesired types to list them later like this example:

VALID_CONTENT_TYPES = ['image/jpeg']
cleaned_content_types = ['image/png', 'image/jpeg', 'image/gif', 'image/jpeg']

undesired_types = cleaned_content_types - VALID_CONTENT_TYPES
if undesired_types.size > 0
error_message = "The types #{undesired_types.join(', ')} are not allowed"
else
# The happy path here
end

How can I tell if my string contains a substring from an array?

Given:

arr = ["a", "b", "x"]
str = "DoDox"

EDIT: As the other comments have pointed out, this is actually the fastest way, as it breaks the evaluation when a single true statement is found:

arr.any?{|substr| str.include?(substr)}

The foldl way: I have used this example to illustrate the power of folding expresions. This solution might be somewhat elegant as an illustrative example for foldings, but it is not sufficiently performant.

arr.map{|substr| str.include?(substr)}.inject{|x,y| x || y}

The map-function maps all possible substrings the original string str can have. inject iterates over the returning array and compares its constituents, using a logical or. If any substring in the array 'arr' is encountered, the result will yield true in the result of map. With the or-relation we return a true value, if any value of the result of map was true. This is slower than any?, because every single value in the array gets evaluated even if a true is encountered (wich would always yield a true at the end of inject).

["a", "b", "x"] -------> [false, false, true] ----------> true
map() inject()

How to check in ruby if an string contains any of an array of strings

There you go using #any?.

a = ['key','words','to', 'check']
b = "this is a long string"
a.any? { |s| b.include? s }

Or something like using ::union. But as per the need, you might need to change the regex, to some extent it can be done. If not, then I will go with above one.

a = ['key','words','to', 'check'] 
Regexp.union a
# => /key|words|to|check/
b = "this is a long string"
Regexp.union(a) === b # => false

Check if string contains substring in array AND get the match

str = "foobazbar"
arr = ["baz", "clowns"]
result = arr.find { |s| str.include?(s) }

result at this point is the first element in arr that is a substring of str or is nil

Check if a string contains any substrings from an array

Your solution is efficient, it's just not as expressive as Ruby allows you to be. Ruby provides the Enumerable#any? method to express what you are doing with that loop:

words.any? { |word| sentence.include?(word) }

Determining if a string contains any of the value that is present in an array in ruby

Here's how I would write it:

my_string = "testing.facebook.com"
array = ["google.com","facebook.com","github.com"]

array.any? { |item| my_string.include?(item) }

This uses Enumerable#any?.

Enumerable is a module which is included in the Array class, amongst others.

How to check whether a string contains a substring in Ruby

You can use the include? method:

my_string = "abcdefg"
if my_string.include? "cde"
puts "String includes 'cde'"
end

Determining if an array of strings contains a certain substring in ruby

a.any? should do the job.

> a = ['cat','dog','elephant']
=> ["cat", "dog", "elephant"]
> a.any? { |s| s.include?('ele') }
=> true
> a.any? { |s| s.include?('nope') }
=> false

How to check whether an array contains a substring within a string in Ruby?

string_array.any? { |e| e.include? 'id' }

any? returns true if any of the elements in the array are true for the given predicate (i.e. the block passed to any?)



Related Topics



Leave a reply



Submit