[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Case sensitive strings

Stuart Clarke

5/21/2009 11:34:00 AM

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
--
Posted via http://www.ruby-....

3 Answers

Jan-Erik R.

5/21/2009 11:40:00 AM

0

Stuart Clarke schrieb:
> 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"

;)

Stuart Clarke

5/21/2009 11:54:00 AM

0

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

badboy wrote:
> Stuart Clarke schrieb:
>> I then have a conditional statement to check if a value matches word eg
>>
>> 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"
>
> ;)

--
Posted via http://www.ruby-....

Rob Biedenharn

5/21/2009 1:45:00 PM

0

On May 21, 2009, at 7:54 AM, Stuart Clarke wrote:
> 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
>
> badboy wrote:
>> Stuart Clarke schrieb:
>>> I then have a conditional statement to check if a value matches
>>> word eg
>>>
>>> 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"
>>
>> ;)
>
> --

Or just:

yourword.casecmp(foo).zero?

See String#casecmp for more.

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com