473,804 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2846
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(l hs,rhs, ComparerOptions .OrdinalIgnoreC ase) is the
fastest.

You can also use StringComparer. OrdinalIgnoreCa se.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, StringCompariso n.OrdinalIgnore Case); // or
InvariantCultur eIgnoreCase

string.Equals(l hs, rhs, StringCompariso n.OrdinalIgnore Case) // or
InvariantCultur eIgnoreCase

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

etc

Marc

Dec 12 '06 #3
ComparerOptions .OrdinalIgnoreC ase
I meant StringCompariso n.OrdinalIgnore Case, 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.C ompare(string a, string b, bool ignoreCase);

or

System.Collecti on.CaseInsensit iveComparer.Def aultInvariant.C ompare(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**********@g mail.comwrote:
In 2.0, IIRC, from tests (now deleted) I believe that
string.Equals(l hs,rhs, ComparerOptions .OrdinalIgnoreC ase) is the
fastest.

You can also use StringComparer. OrdinalIgnoreCa se.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.co m>
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.co mwrote in message
news:MP******** *************** *@msnews.micros oft.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( )?

///ark
Dec 12 '06 #8
Mark Wilden <mw*****@commun itymtm.comwrote :
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.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 :)

using System;
using System.Globaliz ation;
using System.Threadin g;

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

Thread.CurrentT hread.CurrentCu lture = info;

Console.WriteLi ne ("mail".ToUpper ()=="MAIL");
Console.WriteLi ne ("mail".ToUpper ()=="MAIL".ToUp per());
}
}

ToLower() doesn't work either.

Isn't i18n fun? :)

--
Jon Skeet - <sk***@pobox.co m>
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

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

Similar topics

2
10317
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 allow query string compares to be case insensitive. We have identified DB2 forces all query string compares to be evaluated case sensitive. Since our code is written to assume case insensitivity, it curently seems that we must change our code...
17
4676
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 analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if( someString.ToLower() == "somestring" ) and
4
23973
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
1926
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 want for this loop to run as fast as possible. So I'm looking for a method that would be faster than .ToUpper(). Thanks!
1
35419
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 default NULL, `poz_nazwa` varchar(40) collate latin2_bin NOT NULL,
7
5475
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
5103
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 the case insensitive comparer ?
7
3343
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 way or any gotcha's from my code below.
14
24285
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 MultiByte String env (MBCS) and wchar_t if UNICODE #if defined(WIN32) || defined(UNDER_CE)
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.