Ruby是另一个字符串中的字符串(使用正则表达式)?

时间:2012-01-23 22:19:51

标签: ruby-on-rails ruby

ruby​​中是否有present?方法来检查另一个字符串是否存在?我想在找到匹配后立即返回,因为我将检查多个子串。谢谢!

4 个答案:

答案 0 :(得分:2)

我相信你的意思是include?

答案 1 :(得分:1)

我相信你正在寻找包括?

"ab123de".include?("123")

答案 2 :(得分:0)

答案 3 :(得分:0)

抱歉,找到了我要找的东西(因为使用正则表达式):

  

index(regexp [,offset])→fixnum或nil

     

返回的索引   在str中第一次出现给定的子串或模式(regexp)。   如果找不到则返回nil。

"hello".index('lo')            #=> 3
"hello".index('a')             #=> nil
"hello".index(?e)              #=> 1
"hello".index(/[aeiou]/, -3)   #=> 4

http://ruby-doc.org/core-1.9.3/String.html

我只是使用循环检查索引是否为nil