开发者

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

开发者 https://www.devze.com 2022-12-17 04:38 出处:网络
I\'m familiar with Ruby\'s include? method for strings, but how can I chec开发者_JAVA技巧k a string for multiple things?

I'm familiar with Ruby's include? method for strings, but how can I chec开发者_JAVA技巧k 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消