【发布时间】:2012-02-17 06:18:16
【问题描述】:
ruby 中是否有 present? 方法来检查字符串是否存在于另一个字符串中?我想在找到匹配项后立即返回,因为我将检查多个子字符串。谢谢!
【问题讨论】:
标签: ruby-on-rails ruby
ruby 中是否有 present? 方法来检查字符串是否存在于另一个字符串中?我想在找到匹配项后立即返回,因为我将检查多个子字符串。谢谢!
【问题讨论】:
标签: ruby-on-rails ruby
我相信你的意思是include?
【讨论】:
我相信您正在寻找包含?
"ab123de".include?("123")
【讨论】:
抱歉,找到了我要找的东西(因为使用了 regexp):
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
我只是使用循环检查索引是否为零
【讨论】:
'hello'['lo'] 或 'hello'[/lo/]。