Is there a better way to ignore uppercase letters than this?
"Hello".start_with?("hell","Hell") #=> true
I want to check if a string element in an array starts with another string ignoring an uppercase letter, like LIKE % in MySQL.
LIKE %
I would do something like this:
'Hello'.upcase.start_with?('HELL')
Another approach to the same problem. This is equivalent to doing something like UPPER(column) like 'SOMETHING%' in SQL.
UPPER(column) like 'SOMETHING%'
You can use regex with String#=~ :
String#=~
"Hello" =~ /^hell/i #=> 0 "hELLO" =~ /^hell/i #=> 0 "world" =~ /^hell/i #=> nil
Since 0 is true and nil is false, this can be used in the if clause:
0
nil
if
if str =~ /^hell/i # starts with hell end
I think it's best to use a combination of Ruby regular expressions with the ignore flag flag:
'Hello'.match /^hell/i
"^" indicates the beginning of a line. Without it, it will correspond to "hell" anywhere in the line. And the last āiā is just a regular expression flag indicating a match for the ignore character set.
Information on the Ruby Regex API can be found here: http://www.regular-expressions.info/ruby.html
Source: https://habr.com/ru/post/1498582/More articles:What would be the best way to write this in JS / jQuery? (Several separate objects with several fields) - optimizationStrange behavior of the Android camera when the torch is turned on - androidWhy Get-Member does not give a complete list of all properties and methods. - powershellVideoView crashes after a long time - androidHow can I give a key value as a parameter to the install () method? - appletC # add PayPal donation button in app - c #Know if a DOM object is dead - javascriptChecking form sections - jquerySign a line in open source - c ++Confirm two forms when one view depends on another - javascriptAll Articles