Fast Case Insensitive String Comparisons

Discussion in 'ASP .Net' started by Merk, Dec 12, 2006.

  1. Merk

    Merk Guest

    What are some alternatives to using .ToUpper() to perform case insensitive
    string comparisons?

    The reason I'm asking is that I'm comparing strings in a long loop, looking
    for equality; and I want for this loop to run as fast as possible. So I'm
    looking for a method that would be faster than .ToUpper().

    Thanks!
     
    Merk, Dec 12, 2006
    #1
    1. Advertising

  2. Merk

    Andy Guest

    Did you try specifying case insensitivity in the .Compare method?

    Merk wrote:
    > What are some alternatives to using .ToUpper() to perform case insensitive
    > string comparisons?
    >
    > The reason I'm asking is that I'm comparing strings in a long loop, looking
    > for equality; and I want for this loop to run as fast as possible. So I'm
    > looking for a method that would be faster than .ToUpper().
    >
    > Thanks!
     
    Andy, Dec 12, 2006
    #2
    1. Advertising

  3. Merk

    Marc Gravell Guest

    In 2.0, IIRC, from tests (now deleted) I believe that
    string.Equals(lhs,rhs, ComparerOptions.OrdinalIgnoreCase) is the
    fastest.

    You can also use StringComparer.OrdinalIgnoreCase.Equals(...) but I
    beleive that this is a little slower.

    Your best bet is to try every option in a tight loop to test;
    you could try:

    lhs.ToUpper() == rhs.ToUpper()

    lhs.Equals(rhs, StringComparison.OrdinalIgnoreCase); // or
    InvariantCultureIgnoreCase

    string.Equals(lhs, rhs, StringComparison.OrdinalIgnoreCase) // or
    InvariantCultureIgnoreCase

    StringComparer.OrdinalIgnoreCase.Equals(lhs, rhs); // or invariant case
    insensitive

    etc

    Marc
     
    Marc Gravell, Dec 12, 2006
    #3
  4. Merk

    Marc Gravell Guest

    > ComparerOptions.OrdinalIgnoreCase
    I meant StringComparison.OrdinalIgnoreCase, but intellisense would have
    told you that...

    Marc
     
    Marc Gravell, Dec 12, 2006
    #4
  5. Merk

    Guest

    Merk wrote:
    > What are some alternatives to using .ToUpper() to perform case insensitive
    > string comparisons?
    >
    > The reason I'm asking is that I'm comparing strings in a long loop, looking
    > for equality; and I want for this loop to run as fast as possible. So I'm
    > looking for a method that would be faster than .ToUpper().
    >
    > Thanks!


    System.String.Compare(string a, string b, bool ignoreCase);

    or

    System.Collection.CaseInsensitiveComparer.DefaultInvariant.Compare(string
    a, string b);

    anyone have any ideas about my mulitple browser problem?
     
    , Dec 12, 2006
    #5
  6. Merk

    Bruce Wood Guest

    Merk wrote:
    > What are some alternatives to using .ToUpper() to perform case insensitive
    > string comparisons?
    >
    > The reason I'm asking is that I'm comparing strings in a long loop, looking
    > for equality; and I want for this loop to run as fast as possible. So I'm
    > looking for a method that would be faster than .ToUpper().


    Are you comparing the same string over and over again? For example, are
    you sorting an array? If you are, then store the strings in both their
    uppercase and mixed case versions, and compare only the uppercase
    versions. You'll incur the cost of uppercasing them only once, and then
    get the payback on the comparisons. Trade memory for more speed.

    If you test each string only once then, of course, this won't help.

    Usually you gain efficiencies when you step back and look at the
    overall problem, and how you can avoid doing the same work over and
    over again, rather than trying to figure out how to do that work faster.
     
    Bruce Wood, Dec 12, 2006
    #6
  7. Marc Gravell <> wrote:
    > In 2.0, IIRC, from tests (now deleted) I believe that
    > string.Equals(lhs,rhs, ComparerOptions.OrdinalIgnoreCase) is the
    > fastest.
    >
    > You can also use StringComparer.OrdinalIgnoreCase.Equals(...) but I
    > beleive that this is a little slower.
    >
    > Your best bet is to try every option in a tight loop to test;
    > you could try:
    >
    > lhs.ToUpper() == rhs.ToUpper()


    Note that this test is not a culture-safe one. For instance, in Turkey,
    I believe (if I remember the bug I had to fix in a system a while ago
    :) that "mail".ToUpper() != "MAIL".

    Using a StringComparer is a much better way, IMO.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Dec 12, 2006
    #7
  8. Merk

    Mark Wilden Guest

    "Jon Skeet [C# MVP]" <> wrote in message
    news:...
    >
    > Note that this test is not a culture-safe one. For instance, in Turkey,
    > I believe (if I remember the bug I had to fix in a system a while ago
    > :) that "mail".ToUpper() != "MAIL".


    Just out of curiosity, did "mail".ToUpper() == "MAIL".ToUpper()?

    ///ark
     
    Mark Wilden, Dec 12, 2006
    #8
  9. Mark Wilden <> wrote:
    > "Jon Skeet [C# MVP]" <> wrote in message
    > news:...
    > >
    > > Note that this test is not a culture-safe one. For instance, in Turkey,
    > > I believe (if I remember the bug I had to fix in a system a while ago
    > > :) that "mail".ToUpper() != "MAIL".

    >
    > Just out of curiosity, did "mail".ToUpper() == "MAIL".ToUpper()?


    Nope :)

    using System;
    using System.Globalization;
    using System.Threading;

    class Test
    {
    static void Main()
    {
    CultureInfo info = CultureInfo.CreateSpecificCulture("tr-TR");

    Thread.CurrentThread.CurrentCulture = info;

    Console.WriteLine ("mail".ToUpper()=="MAIL");
    Console.WriteLine ("mail".ToUpper()=="MAIL".ToUpper());
    }
    }

    ToLower() doesn't work either.

    Isn't i18n fun? :)

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Dec 12, 2006
    #9
  10. Merk

    Marc Gravell Guest

    Good to know; cheers for the input Jon.

    For ref, I only mentioned the ToUpper() as a performance comparison
    (since the OP explicitely mentioned it) to the StringComparer and
    string.Equals() [with stated comparison], but thanks for the heads-up
    and "proof positive" example.

    Marc
     
    Marc Gravell, Dec 12, 2006
    #10
  11. Jon Skeet [C# MVP] <> wrote:
    >Mark Wilden <> wrote:
    >> Just out of curiosity, did "mail".ToUpper() == "MAIL".ToUpper()?

    >Nope :)


    Funny!

    The issue was that lowercase "i" gets capitalised to U+0130, "Latin
    Capital Letter I With Dot Above".

    Instead of the more normal U+0049, "Latin Capital Letter I".


    I'm curious! Are there any Turks here who can explain Turkish
    capitalisation?

    --
    Lucian
     
    Lucian Wischik, Dec 12, 2006
    #11
  12. Merk

    Mark Wilden Guest

    >> "Jon Skeet [C# MVP]" <> wrote in message
    >> news:...
    >> >
    >> > Note that this test is not a culture-safe one. For instance, in Turkey,
    >> > I believe (if I remember the bug I had to fix in a system a while ago
    >> > :) that "mail".ToUpper() != "MAIL".

    >>
    >> Just out of curiosity, did "mail".ToUpper() == "MAIL".ToUpper()?

    >
    > Nope :)


    Oh well - I guess it's nobody's business but the Turks'.

    ///ark
     
    Mark Wilden, Dec 13, 2006
    #12
  13. Mark Wilden <> wrote:
    > > Nope :)

    >
    > Oh well - I guess it's nobody's business but the Turks'.


    Are you suggesting a history-insensitive comparison?

    StringComparer.IgnoreHistory.Equals("Istanbul". "Constantinople")

    Next up: a "man" comparison: Man.Triangle > Man.Particle etc?

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Dec 13, 2006
    #13
    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. Tee
    Replies:
    3
    Views:
    7,819
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  2. Guoqi Zheng

    string.indexof case insensitive

    Guoqi Zheng, Jul 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    12,960
    bentangle
    Jan 25, 2009
  3. Ville Vainio
    Replies:
    11
    Views:
    478
    Ville Vainio
    Apr 6, 2005
  4. Replies:
    1
    Views:
    2,485
    Mark P
    Apr 6, 2007
  5. Xah Lee
    Replies:
    4
    Views:
    984
Loading...

Share This Page