tags:

views:

397

answers:

1

I'm familiar with Ruby's include? method for strings, but how can I check a string for multiple things?

Specifically, I need to check if a string contains "Fwd:" or "FW:" (and should be case insensitive)

Example string would be: "FWD: Your Amazon.com Order Has Shipped"

+9  A: 
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).

sepp2k
Thanks. Using the regex...how would you translate that line to say "if the_string does NOT have fwd:|fw:"?
Shpigford
And also, is there an official term for the use of `=~`
Shpigford
There's the `!~` operator which is exactly the same as negating the result of `=~`. Using `=~` is usually just referred to as matching against a regex. Don't know whether that counts as an official term.
sepp2k