简体   繁体   中英

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

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM