"str1" == "STR1" case insensitive

S

Shea Martin

For doing case insensitive comparisons, I have been using

if str1.casecmp(str2)==0
puts 'equal'
else
puts 'not equal'
end

Is their a way to do this with simpler syntax?

~S
 
J

Jacob Fugal

For doing case insensitive comparisons, I have been using

if str1.casecmp(str2)=3D=3D0
puts 'equal'
else
puts 'not equal'
end

Is their a way to do this with simpler syntax?

I find String#downcase convenient for these sorts of comparisons:

if str1.downcase =3D=3D str2.downcase
puts 'equal'
else
puts 'not equal'
end

Jacob Fugal
 
1

13

Hi,

My suggestion is to write a simple convinience method such as:

def are_equal(str1, str2)
str1.casecmp(str2) =3D=3D 0
end

irb(main):023:0> if are_equal('foo', 'Foo')
irb(main):024:1> puts 'equal'
irb(main):025:1> else
irb(main):026:1* puts 'not equal'
irb(main):027:1> end
equal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top