Ruby: How to Check If a String Contains Multiple Items

Check whether a string contains one of multiple substrings

Try parens in the expression:

 haystack.include?(needle1) || haystack.include?(needle2)

Ruby: Check if a string contains multiple values

Write as below :

val = "abc123"
val[/[1af]/] # => "a"
val[/[w6f]/] # => nil

So change your code as

if val[/[a23]/]
#...
end

See the documentation of str[regexp] → new_str or nil.

If a Regexp is supplied, the matching portion of the string is returned. If a capture follows the regular expression, which may be a capture group index or name, follows the regular expression that component of the MatchData is returned instead.

Ruby: How to check if a string contains multiple items?

the_string =~ /fwd:|fw:/i

You could also use something like

%w(fwd: fw:).any? {|str| the_string.downcase.include? str}

Though personally I like the version using the regex better in this case (especially as you have to call downcase in the second one to make it case insensitive).

Check if a string is any of multiple items

names.include?(your_string)

If the string is inside the array it will return true

Check if a string contains multiple items, separated by comma or space?

a = "/contact, /support, /about".split(/[,\s]+/)
# => ["/contact", "/support", "/about"]

a.any?{|s| "http://example.com/contact".include?(s)}
# => true

Ruby Check condition if a string include multi-different strings?

Yes, as below :

if %w(string1 string2 string3).any? { |s| my_string.include? s }
# your code
end

Here is the documentation : Enumerable#any?

Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } that will cause any? to return true if at least one of the collection members is not false or nil.

Here is another way ( more fastest) :

[13] pry(main)> ary = %w(string1 string2 string3)
=> ["string1", "string2", "string3"]
[14] pry(main)> Regexp.union(ary)
=> /string1|string2|string3/
[15] pry(main)> "abbcstring2"[Regexp.union(ary)]
=> "string2"
[16] pry(main)> "abbcstring"[Regexp.union(ary)]
=> nil

Just read Regexp::union and str[regexp] → new_str or nil .

Check whether a string contains all the characters of another string in Ruby

Sets and array intersection don't account for repeated chars, but a histogram / frequency counter does:

require 'facets'

s1 = "aasmflathesorcerersnstonedksaottersapldrrysaahf"
s2 = "harrypotterandtheasorcerersstone"
freq1 = s1.chars.frequency
freq2 = s2.chars.frequency
freq2.all? { |char2, count2| freq1[char2] >= count2 }
#=> true

Write your own Array#frequency if you don't want to the facets dependency.

class Array
def frequency
Hash.new(0).tap { |counts| each { |v| counts[v] += 1 } }
end
end

Check if a string contains multiple occurrences of a character in Ruby?

String has a .count method which takes in a character.

str.count('.') == 3

EDIT:

Since you are looking to match an IP address, www.regular-expressions.info has an example of capturing an IP using Regular Expressions.

if possible_ip_address.match(%r{\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b})
# code here...
end

This will capture addresses that aren't valid (such as 999.999.0.1) but it will prevent false positives like

"not.real.ip.address".count('.') == 3 #=> true

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


Related Topics



Leave a reply



Submit