Check whether a string contains one of multiple substrings

后端 未结 7 1295
挽巷
挽巷 2020-12-23 19:32

I\'ve got a long string-variable and want to find out whether it contains one of two substrings.

e.g.

haystack = \'this one is pretty long\'
needle1          


        
7条回答
  •  -上瘾入骨i
    2020-12-23 20:03

    For an array of substrings to search for I'd recommend

    needles = ["whatever", "pretty"]
    
    if haystack.match(Regexp.union(needles))
      ...
    end
    

提交回复
热议问题