By default the Ruby String#end_with?
method does a case-sensitive match. However, you can perform a case-insensitive match by:
- Converting both, the string and the suffix(es) you are comparing, to lowercase (or uppercase), and;
- Applying
String#end_with?
on the converted strings.
For example:
puts "fooBar".downcase.end_with?("bar") #=> true puts "fooBAR".downcase.end_with?("foo", "bar", "baz") #=> true # ...
In the examples above, the strings are converted to lowercase first (using the String#downcase
method), and then String#end_with?
is applied on the converted strings.
You can also use the String#upcase
method instead of String#downcase
if you prefer to convert the strings to uppercase instead of lowercase.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.