Is there a better way to ignore uppercase than this?
"Hello".start_with?("hell","Hell") #=> true
I want to check if a string element in an array starts with another string ignoring uppercase, like LIKE %
in MySQL.
我会做这样的事情:
'Hello'.upcase.start_with?('HELL')
解决同一问题的另一种方法。UPPER(column) like 'SOMETHING%'
这相当于在 SQL中做类似的事情。
我认为最好的方法是使用 Ruby 的正则表达式匹配忽略大小写标志:
'Hello'.match /^hell/i
'^' 指定字符串的开头。没有它会匹配字符串中任何地方的“地狱”。最后一个“i”只是一个正则表达式标志,表示与忽略大小写集匹配。
您可以在此处找到有关 Ruby Regex API 的更多信息: http ://www.regular-expressions.info/ruby.html