How to compare strings ignoring the case
I want apple
and Apple
comparison to be true
.
Currently
"Apple" == "Apple" # returns TRUE
"Apple" == "APPLE" # returns FALSE
You're looking for casecmp
. It returns 0
if two strings are equal, case-insensitively.
str1.casecmp(str2) == 0
"Apple".casecmp("APPLE") == 0
#=> true
Alternatively, you can convert both strings to lower case (str.downcase
) and compare for equality.
In Ruby 2.4.0 you have: casecmp?(other_str) → true, false, or nil
"abcdef".casecmp?("abcde") #=> false
"aBcDeF".casecmp?("abcdef") #=> true
"abcdef".casecmp?("abcdefg") #=> false
"abcdef".casecmp?("ABCDEF") #=> true
Here you have more info