本文介绍了检查一个字符串是否包含多个子字符串之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的字符串变量,想知道它是否包含两个子字符串之一.

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

例如

haystack = 'this one is pretty long'
needle1 = 'whatever'
needle2 = 'pretty'

现在,我需要像这样的析取符,尽管它在Ruby中不起作用:

Now I'd need a disjunction like this which doesn't work in Ruby though:

if haystack.include? needle1 || haystack.include? needle2
    puts "needle found within haystack"
end

推荐答案

尝试在表达式中使用括号:

Try parens in the expression:

 haystack.include?(needle1) || haystack.include?(needle2)

这篇关于检查一个字符串是否包含多个子字符串之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:41