我想知道什么是使String.include?方法忽略大小写的最佳方法.目前我正在做以下事情.有什么建议?谢谢!
a = "abcDE"
b = "CD"
result = a.downcase.include? b.downcase
Run Code Online (Sandbox Code Playgroud)
编辑:怎么样Array.include?.数组的所有元素都是字符串.
Phr*_*ogz 58
如果您只是针对数组测试单个单词,或者数组内容经常更改,那么最快的答案是Aaron:
array.any?{ |s| s.casecmp(mystr)==0 }
Run Code Online (Sandbox Code Playgroud)
如果你要针对静态数组测试很多单词,那么使用farnoy答案的变体要好得多:创建一个包含单词全小写版本的数组副本,然后使用include?.(这假设您可以节省内存来创建阵列的变异副本.)
# Do this once, or each time the array changes
downcased = array.map(&:downcase)
# Test lowercase words against that array
downcased.include?( mystr.downcase )
Run Code Online (Sandbox Code Playgroud)
更好的是,Set从你的阵列创建一个.
# Do this once, or each time the array changes
downcased = Set.new array.map(&:downcase)
# Test lowercase words against that array
downcased.include?( mystr.downcase )
Run Code Online (Sandbox Code Playgroud)
我在下面的原始答案是一个非常差的表演者,通常不合适.
以下是使用随机套管查找1,000个单词的基准测试,其中包含超过100,000个单词的数组,其中500个单词将被找到而500个单词将不会被找到.
any?.any?我的评论,'casecmp'测试是Arron的答案.Set在测试之前从下层字符串数组中创建一个. user system total real
regex 18.710000 0.020000 18.730000 ( 18.725266)
casecmp 5.160000 0.000000 5.160000 ( 5.155496)
downarray 16.760000 0.030000 16.790000 ( 16.809063)
downonce 0.650000 0.000000 0.650000 ( 0.643165)
set_once 0.040000 0.000000 0.040000 ( 0.038955)
Run Code Online (Sandbox Code Playgroud)
如果您可以创建一个单个下载的数组副本一次以执行许多查找,farnoy的答案是最好的(假设您必须使用数组).Set但是,如果你可以创建一个,那就去做吧.
如果您愿意,请检查基准测试代码.
我(最初说我)将亲自创建一个不区分大小写的正则表达式(对于字符串文字)并使用:
re = /\A#{Regexp.escape(str)}\z/i # Match exactly this string, no substrings
all = array.grep(re) # Find all matching strings…
any = array.any?{ |s| s =~ re } # …or see if any matching string is present
Run Code Online (Sandbox Code Playgroud)
一旦找到单个匹配,使用any?可以稍微快于grep它可以退出循环.
far*_*noy 11
对于数组,请使用:
array.map(&:downcase).include?(string)
Run Code Online (Sandbox Code Playgroud)
Regexps非常慢,应该避免.
您可以使用casecmp进行比较,忽略大小写.
"abcdef".casecmp("abcde") #=> 1
"aBcDeF".casecmp("abcdef") #=> 0
"abcdef".casecmp("abcdefg") #=> -1
"abcdef".casecmp("ABCDEF") #=> 0
Run Code Online (Sandbox Code Playgroud)