Kerry Buckley What’s the simplest thing that could possibly go wrong?

8 November 2007

String#casecmp

Filed under: Java,Ruby — Kerry Buckley @ 1:31 pm

As far as I can tell, this is the correct way to do case-insensitive string comparison in Ruby:

if string1.casecmp(string2) == 0
  # strings match, ignoring case
end

That’s so ugly, it almost looks like Java. Actually, it’s worse than Java, which has String#equalsIgnoreCase.

4 Comments »

  1. We need to do that for Mojo too as .NET creates the hex digest in upper case =/

    Comment by Robbie — 8 November 2007 @ 2:19 pm

  2. Yeah yeah, already done :-) – that’s where I found it.

    Comment by Kerry — 8 November 2007 @ 3:02 pm

  3. This looks better to me:

    string1.downcase == string2.downcase

    Comment by Brian Marick — 8 November 2007 @ 3:18 pm

  4. You could use regular expressions:

    string1 =~ /^string2$/i

    Comment by Alkesh — 12 November 2007 @ 4:00 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress