Case sensitive strings

S

Stuart Clarke

Hi all,

This is one of those annoying ones, but simple to solve I would imagine.

I have a word list, where each entry is on a single line and in
lowercase. I process this text file reading each word

words.each |word|
etc etc

I then have a conditional statement to check if a value matches word eg

if foo == word
puts foo
end

This all works well, but I am stuck on how to deal with upper and
lowercase. I want to check if foo is equal to word in lowercase and in
uppercase.

What is the best way to do this?

Many thanks

Stuart
 
B

badboy

Stuart said:
Hi all,

This is one of those annoying ones, but simple to solve I would imagine.

I have a word list, where each entry is on a single line and in
lowercase. I process this text file reading each word

words.each |word|
etc etc

I then have a conditional statement to check if a value matches word eg

if foo == word
puts foo
end

This all works well, but I am stuck on how to deal with upper and
lowercase. I want to check if foo is equal to word in lowercase and in
uppercase.

What is the best way to do this?

Many thanks

Stuart
you can use regexes and the "i"-flag:
foo =~ /^yourword$/i
(or better /\Ayourword\Z/i if you're not sure if there are newlines in it)
or lowercase foo:
foo.downcase == "yourword"

;)
 
S

Stuart Clarke

Thanks for a quick reply. I had tried the first one but it didn't work.

Did not think of the second one, silly me.

Thanks a lot
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top