19

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"

1 Answer 1

30
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).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Using the regex...how would you translate that line to say "if the_string does NOT have fwd:|fw:"?
And also, is there an official term for the use of =~
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.