473,405 Members | 2,282 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,405 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 2804
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

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.