Case-insensitive string compare in .NET

I've been writing code full-time in C# for about 4 years now but every once in a while someone shows me something totally new.  Last week, one of the devs on my team showed me an override of String.Equals() that does a case-insensitive compare.  It kinda blew my mind that I'd never seen that before.

The following line of code evaluates to true.

string.Equals("string number 1", "String NUMBER 1", StringComparison.CurrentCultureIgnoreCase) 

There is also a similar override of String.Compare().

Pretty handy, huh? 

-Ben

posted @ Wednesday, May 17, 2006 9:02 AM

Print

Comments on this entry:

# re: Case-insensitive string compare in .NET

Left by John at 10/9/2007 10:33 AM
Gravatar
Thanks buddy exactly what I was looking for :) and yes you learn new thing everyday in programming as I did today.

# re: Case-insensitive string compare in .NET

Left by Muhammad Atif at 1/28/2008 10:32 AM
Gravatar
Thats wat i was looking...MSDN needs to be reveiwed:)

Thanks budy

# re: Case-insensitive string compare in .NET

Left by Thomas at 2/20/2008 6:21 AM
Gravatar
What about this:
string.Compare(("string number 1", "String NUMBER 1", true) == 0

So long!

# badaruniter 12 post

Left by badaruniter blog at 3/25/2008 7:07 PM
Gravatar
all about badaruniter and top news

# re: Case-insensitive string compare in .NET

Left by SoMoS at 6/13/2008 4:20 AM
Gravatar
Something as usual as making a software case -insensitive becomes so hard because there are a lot of string comparing methods that can not let you specify with kind of comparision do you want to do, like:

Array.IndexOf() , = , etc

I think that should be a generic option like Option Compare CaseInsensitive that would be great!

# re: Case-insensitive string compare in .NET

Left by Sean at 6/19/2008 3:00 PM
Gravatar
Another option that we use is to use the .ToUpper or .ToLower on each variable or string in the comparison.

# re: Case-insensitive string compare in .NET

Left by Lollo at 7/17/2008 10:04 AM
Gravatar
Here my solution:

string unitLetter;
List<string> deviceAdded = new List<string>();
CaseInSensitiveEqualityComparer caseInSensitiveEqualityComparer = new CaseInSensitiveEqualityComparer();
..................

//code to change the value of unitLetter
..................

if (!deviceAdded.Contains(unitLetter, caseInSensitiveEqualityComparer))
{
deviceAdded.Add(unitLetter);
}


#region EqualityComparer
public class CaseInSensitiveEqualityComparer : IEqualityComparer<string>
{
// Implement the IComparable interface.
public bool Equals(string obj1, string obj2)
{
if (string.Compare(obj1, obj2, true) == 0)
{
return true;
}
else
{
return false;
}
}

public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
#endregion EqualityComparer

# re: Case-insensitive string compare in .NET

Left by Kerala at 7/22/2008 2:33 AM
Gravatar
I was looking for this for a long time. Thanks. But when I compile it error occured ---->The type or namespace name 'StringComparison' could not be found (are you missing a using directive or an assembly reference?)

# re: Case-insensitive string compare in .NET

Left by Marc Delahousse at 12/11/2008 12:03 PM
Gravatar
I am working in C++ but this applies to the topic.

I've often com across bugs where developers forgot to take case insensitivity into comparison. As a result, these bugs show up again and again. So Ièm currently working on a CaseeInsensitiveString, this avoids the proplem altogether by forcing the USE of the string to be case insensitive. This is great for things like IDs, paths, and people's names.

# re: Case-insensitive string compare in .NET

Left by RyanB at 1/8/2009 1:02 PM
Gravatar
@Sean, absolutely not!

That is precisely why code breaks. When your software runs on a different system, StringComparison.CurrentCultureIgnoreCase and its peers are the only way to go. ToUpper() and ToLower() comparison methods fail in many instances outside of en-US.

# re: Case-insensitive string compare in .NET

Left by Alaa at 2/15/2009 10:19 AM
Gravatar
searched all the web for that ;)
thanks mate.

# re: Case-insensitive string compare in .NET

Left by Xcalibur at 5/13/2009 10:22 PM
Gravatar
A ToUpper() approach should definitely not be used - it is culture insensitive, as pointed out above, and is approximately 4-5 times slower than using String.Compare(). There are metrics on the net that demonstrate this

# re: Case-insensitive string compare in .NET

Left by steve at 6/10/2009 8:44 PM
Gravatar
Hi mate,
Just stumbled across your blog and just thought I'd point out the solution left by Lollo has a slightly faulty implementation of GetHashCode().
Since it's case insensitive, it should be returning the same hash code for "Test" and "test".

Instead, consider using StringComparer

public class CaseInsensitiveEqualityComparer : IEqualityComparer<string>
{
StringComparer _comparer = StringComparer.Create(CultureInfo.CurrentCulture, true);

#region IEqualityComparer<string> Members

public bool Equals(string x, string y)
{
return _comparer.Compare(x, y);
}

public int GetHashCode(string obj)
{
return _comparer.GetHashCode(obj);
}

#endregion
}

# re: Case-insensitive string compare in .NET

Left by steve at 6/10/2009 8:48 PM
Gravatar
Ah I take that previous comment back.
I didn't realise StringComparer implements IEqualityComparer<string>.

As such you can just use
IEqualityComparer<string> caseInsensitiveEqualityComparer = StringComparer.Create(CultureInfo.CurrentCulture, true);

if (!deviceAdded.Contains(unitLetter, caseInsensitiveEqualityComparer))
{
deviceAdded.Add(unitLetter);
}

# re: Case-insensitive string compare in .NET

Left by Unknown at 10/26/2009 4:07 AM
Gravatar
Very Helpful! its totaly new thing for me..

# re: Case-insensitive string compare in .NET

Left by Unknown at 10/26/2009 4:08 AM
Gravatar
Very Helpful! its totaly new thing for me..!!!!!!!

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 1 and type the answer here:
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789