Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Fast Case Insensitive String Comparisons

Reply
Thread Tools

Fast Case Insensitive String Comparisons

 
 
Merk
Guest
Posts: n/a
 
      12-12-2006
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!


 
Reply With Quote
 
 
 
 
Andy
Guest
Posts: n/a
 
      12-12-2006
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!


 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      12-12-2006
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

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      12-12-2006
> ComparerOptions.OrdinalIgnoreCase
I meant StringComparison.OrdinalIgnoreCase, but intellisense would have
told you that...

Marc

 
Reply With Quote
 
bob.abrahamian@qrm.com
Guest
Posts: n/a
 
      12-12-2006

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.DefaultI nvariant.Compare(string
a, string b);

anyone have any ideas about my mulitple browser problem?

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      12-12-2006

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.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12-12-2006
Marc Gravell <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      12-12-2006
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
>
> 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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12-12-2006
Mark Wilden <(E-Mail Removed)> wrote:
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> >
> > 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      12-12-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
check file exists with case sensitive on a case insensitive filesystem Xah Lee Perl Misc 4 04-05-2009 11:21 PM
string.indexof case insensitive Guoqi Zheng ASP .Net 4 01-25-2009 02:07 AM
case insensitive find on case sensitive stl map benhoefer@gmail.com C++ 1 04-06-2007 08:42 PM
Case-insensitive dict, non-destructive, fast, anyone? Ville Vainio Python 11 04-06-2005 06:53 AM
how to case select with case-insensitive string ? Tee ASP .Net 3 06-23-2004 07:40 PM



Advertisments