# Case sensitive strings

**URL:** <https://www.ruby-forum.com/t/case-sensitive-strings/168493>\
**Category:** Ruby\
**Created:** [May 21, 2009, 1:34pm UTC](https://www.ruby-forum.com/t/case-sensitive-strings/168493 "2009-05-21T13:34:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 21, 2009, 1:34pm UTC](https://www.ruby-forum.com/t/case-sensitive-strings/168493/1 "2009-05-21T13:34:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 21, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/case-sensitive-strings/168493/2 "2009-05-21T13:41:49Z")

</div>

Stuart C. 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”

😉

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 21, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/case-sensitive-strings/168493/3 "2009-05-21T13:54:16Z")

</div>

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 C. 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”
> 
> 😉

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 21, 2009, 3:45pm UTC](https://www.ruby-forum.com/t/case-sensitive-strings/168493/4 "2009-05-21T15:45:55Z")

</div>

On May 21, 2009, at 7:54 AM, Stuart C. wrote:

> > > word eg  
> > > foo.downcase == “yourword”
> > 
> > 😉
> 
> –

Or just:

yourword.casecmp(foo).zero?

See String#casecmp for more.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
