不区分大小写的数组#include? [英] Case-insensitive Array#include?
问题描述
我想知道使 String.include?
方法忽略大小写的最佳方法是什么.目前我正在做以下事情.有什么建议么?谢谢!
I want to know what's the best way to make the String.include?
methods ignore case. Currently I'm doing the following. Any suggestions? Thanks!
a = "abcDE"
b = "CD"
result = a.downcase.include? b.downcase
Array.include 怎么样?
.数组的所有元素都是字符串.
How about Array.include?
. All elements of the array are strings.
推荐答案
总结
如果您只想针对数组测试单个单词,或者数组的内容经常更改,那么最快的答案是 Aaron:
Summary
If you are only going to test a single word against an array, or if the contents of your array changes frequently, the fastest answer is Aaron's:
array.any?{ |s| s.casecmp(mystr)==0 }
如果您要针对静态数组测试多个单词,最好使用 farnoy 答案的变体:创建包含单词的全小写版本的数组副本,并使用 include?代码>.(这假设您可以腾出内存来创建数组的变异副本.)
If you are going to test many words against a static array, it's far better to use a variation of farnoy's answer: create a copy of your array that has all-lowercase versions of your words, and use include?
. (This assumes that you can spare the memory to create a mutated copy of your array.)
# Do this once, or each time the array changes
downcased = array.map(&:downcase)
# Test lowercase words against that array
downcased.include?( mystr.downcase )
更好的是,创建一个 Set
来自您的数组.
Even better, create a Set
from your array.
# 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 )
我在下面的原始答案表现非常差,通常不合适.
以下是在略高于 100,000 个单词的数组中查找 1,000 个随机大小写单词的基准,其中 500 个单词会被找到,500 个不会.
Following are benchmarks for looking for 1,000 words with random casing in an array of slightly over 100,000 words, where 500 of the words will be found and 500 will not.
- 'regex' 文本是我的答案,使用
any?
. - 'casecmp' 测试是 Arron 的答案,使用了我评论中的
any?
. - 'downarray' 测试是 farnoy 的答案,为 1,000 个测试中的每一个重新创建一个新的缩减数组.
- 'downonce' 测试是 farnoy 的答案,但只预先创建一次查找数组.
- 'set_once' 测试是在测试前一次从缩小字符串数组中创建一个
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)
如果您可以创建一次数组的单个缩小副本来执行多次查找,farnoy 的答案是最好的(假设您必须使用数组).不过,如果您可以创建 Set
,那就去做吧.
If you can create a single downcased copy of your array once to perform many lookups against, farnoy's answer is the best (assuming you must use an array). If you can create a Set
, though, do that.
如果您愿意,检查基准代码.
我(最初说我)会亲自创建一个不区分大小写的正则表达式(用于字符串文字)并使用它:
I (originally said that I) would personally create a case-insensitive regex (for a string literal) and use that:
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
使用 any?
可能比 grep
稍微快一点,因为它可以在找到单个匹配项后立即退出循环.
Using any?
can be slightly faster than grep
as it can exit the loop as soon as it finds a single match.
这篇关于不区分大小写的数组#include?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!