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

后端 未结 1 833
礼貌的吻别
礼貌的吻别 2021-01-01 17:52

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 cont

相关标签:
1条回答
  • 2021-01-01 18:17
    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 讨论(0)
提交回复
热议问题