Say I have a string str = "Things to do: eat and sleep."
说我有一个字符串str =“要做的事情:吃饭和睡觉。”
How could I check if "do: "
exists in str, case insensitive?
我如何检查str中是否存在“do:”,不区分大小写?
5 个解决方案
#1
19
Like this:
喜欢这个:
puts "yes" if str =~ /do:/i
To return a boolean value (from a method, presumably), compare the result of the match to nil
:
要返回一个布尔值(可能来自一个方法),请将匹配结果与nil进行比较:
def has_do(str)
(str =~ /do:/i) != nil
end
Or, if you don’t like the != nil
then you can use !~
instead of =~
and negate the result:
或者,如果你不喜欢!= nil那么你可以使用!〜而不是=〜并否定结果:
def has_do(str)
not str !~ /do:/i
end
But I don’t really like double negations …
但我真的不喜欢双重否定......
#2
4
In ruby 1.9 you can do like this:
在ruby 1.9中你可以这样做:
str.downcase.match("do: ") do
puts "yes"
end
It's not exactly what you asked for, but I noticed a comment to another answer. If you don't mind using regular expressions when matching the string, perhaps there is a way to skip the downcase part to get case insensitivity.
这不完全是你要求的,但我注意到对另一个答案的评论。如果你不介意在匹配字符串时使用正则表达式,也许有一种方法可以跳过downcase部分以获得不区分大小写。
For more info, see String#match
有关更多信息,请参阅String #matre
#3
3
You could also do this:
你也可以这样做:
str.downcase.include? "Some string".downcase
#4
2
If all I'm looking for is a case=insensitive substring match I usually use:
如果我正在寻找的是一个case = insensitive substring match我通常使用:
str.downcase['do: ']
9 times out of 10 I don't care where in the string the match is, so this is nice and concise.
10次中有9次我不在乎比赛中的字符串在哪里,所以这很简洁。
Here's what it looks like in IRB:
这是IRB的样子:
>> str = "Things to do: eat and sleep." #=> "Things to do: eat and sleep."
>> str.downcase['do: '] #=> "do: "
>> str.downcase['foobar'] #=> nil
Because it returns nil
if there is no hit it works in conditionals too.
因为如果没有命中它会返回nil,它也会在条件中起作用。
#5
0
"Things to do: eat and sleep.".index(/do: /i)
index returns the position where the match starts, or nil if not found
index返回匹配开始的位置,如果未找到则返回nil
You can learn more about index method here: http://ruby-doc.org/core/classes/String.html
您可以在此处了解有关索引方法的更多信息:http://ruby-doc.org/core/classes/String.html
Or about regex here: http://www.regular-expressions.info/ruby.html
或者关于正则表达式:http://www.regular-expressions.info/ruby.html
#1
19
Like this:
喜欢这个:
puts "yes" if str =~ /do:/i
To return a boolean value (from a method, presumably), compare the result of the match to nil
:
要返回一个布尔值(可能来自一个方法),请将匹配结果与nil进行比较:
def has_do(str)
(str =~ /do:/i) != nil
end
Or, if you don’t like the != nil
then you can use !~
instead of =~
and negate the result:
或者,如果你不喜欢!= nil那么你可以使用!〜而不是=〜并否定结果:
def has_do(str)
not str !~ /do:/i
end
But I don’t really like double negations …
但我真的不喜欢双重否定......
#2
4
In ruby 1.9 you can do like this:
在ruby 1.9中你可以这样做:
str.downcase.match("do: ") do
puts "yes"
end
It's not exactly what you asked for, but I noticed a comment to another answer. If you don't mind using regular expressions when matching the string, perhaps there is a way to skip the downcase part to get case insensitivity.
这不完全是你要求的,但我注意到对另一个答案的评论。如果你不介意在匹配字符串时使用正则表达式,也许有一种方法可以跳过downcase部分以获得不区分大小写。
For more info, see String#match
有关更多信息,请参阅String #matre
#3
3
You could also do this:
你也可以这样做:
str.downcase.include? "Some string".downcase
#4
2
If all I'm looking for is a case=insensitive substring match I usually use:
如果我正在寻找的是一个case = insensitive substring match我通常使用:
str.downcase['do: ']
9 times out of 10 I don't care where in the string the match is, so this is nice and concise.
10次中有9次我不在乎比赛中的字符串在哪里,所以这很简洁。
Here's what it looks like in IRB:
这是IRB的样子:
>> str = "Things to do: eat and sleep." #=> "Things to do: eat and sleep."
>> str.downcase['do: '] #=> "do: "
>> str.downcase['foobar'] #=> nil
Because it returns nil
if there is no hit it works in conditionals too.
因为如果没有命中它会返回nil,它也会在条件中起作用。
#5
0
"Things to do: eat and sleep.".index(/do: /i)
index returns the position where the match starts, or nil if not found
index返回匹配开始的位置,如果未找到则返回nil
You can learn more about index method here: http://ruby-doc.org/core/classes/String.html
您可以在此处了解有关索引方法的更多信息:http://ruby-doc.org/core/classes/String.html
Or about regex here: http://www.regular-expressions.info/ruby.html
或者关于正则表达式:http://www.regular-expressions.info/ruby.html