473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Fast Case Insensitive String Comparisons

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!
Dec 12 '06 #1
12 1896
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!
Dec 12 '06 #2
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

Dec 12 '06 #3
ComparerOptions.OrdinalIgnoreCase
I meant StringComparison.OrdinalIgnoreCase, but intellisense would have
told you that...

Marc

Dec 12 '06 #4

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?

Dec 12 '06 #5

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.

Dec 12 '06 #6
Marc Gravell <ma**********@gmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 12 '06 #7
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c 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
Dec 12 '06 #8
Mark Wilden <mw*****@communitymtm.comwrote:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 12 '06 #9
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

Dec 12 '06 #10
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
>Mark Wilden <mw*****@communitymtm.comwrote:
>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
Dec 12 '06 #11
>"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
>news:MP************************@msnews.microsoft. com...
>
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
Dec 13 '06 #12
Mark Wilden <mw*****@communitymtm.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 13 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Relaxin | last post by:
Is there a way to make Postgresql case-INSENSITIVE? Thanks
2
by: Tom | last post by:
Hi, Our development team is adding DB2 8.1 compatibility to our existing application which currently supports SQLServer 2000. Our code is written to take advantage of SQLServer's ability to ...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
4
by: Mark Rae | last post by:
Hi, Is it possible to create a case-insensitive List<stringcollection? E.g. List<stringMyList = new List<string>; MyList.Add("MyString"); So that:
12
by: Merk | last post by:
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...
1
by: iksando | last post by:
Hello, I'm looking for solution in case insensitive search in MySql. Table exemple. CREATE TABLE `pozycje` ( `id` int(11) NOT NULL auto_increment, `eid` varchar(20) collate latin2_bin...
7
by: jpuopolo | last post by:
All: The String class has a CompareTo method, that compares to strings. Is there a similar method that performs case-insensitive comparisons? Thanks, John
2
by: pamela fluente | last post by:
Hi I am doing something like: Dim MiDict As New System.Collections.Generic.Dictionary(Of String, MyObject) how should I change the above so that the string comparison (key) is done using...
7
by: Adrian | last post by:
Hi, I want a const static std::set of strings which is case insensitive for the values. So I have the following which seems to work but something doesnt seem right about it. Is there a better...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.