Case sensitive strings

Discussion in 'Ruby' started by Stuart Clarke, May 21, 2009.

  1. 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-forum.com/.
    Stuart Clarke, May 21, 2009
    #1
    1. Advertising

  2. Stuart Clarke

    badboy Guest

    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"

    ;)
    badboy, May 21, 2009
    #2
    1. Advertising

  3. 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-forum.com/.
    Stuart Clarke, May 21, 2009
    #3
  4. 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://agileconsultingllc.com
    Rob Biedenharn, May 21, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boban Dragojlovic

    DropDownList values are case sensitive?

    Boban Dragojlovic, Nov 6, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,221
    Boban Dragojlovic
    Nov 6, 2003
  2. Martin Dechev
    Replies:
    0
    Views:
    522
    Martin Dechev
    Apr 2, 2004
  3. Replies:
    0
    Views:
    507
  4. Replies:
    1
    Views:
    2,457
    Mark P
    Apr 6, 2007
  5. Xah Lee
    Replies:
    4
    Views:
    931
Loading...

Share This Page