views:

3124

answers:

10

I know that the following is case sensitive:

if (StringA == StringB) {

So is there an operator which will compare two strings in an insensitive manner?

+38  A: 

Try this:

string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
John Feminella
You should add a link, it improves the quality of answers by quite a bit.
Samuel
I'm a relative StackOverflow newbie -- can you explain what you mean by adding a link? Do you mean to the MSDN docs?
John Feminella
gosh you're quick .. lol I was typing the answer you got me first =D
Erick
You can add an MSDN link (which I usually do) or if you find any interesting blog posts or such that maybe expand the answer.
Samuel
And you could enrich your answer by trying to get close to what the asker wanted. In this case, you could post a quick extension method.
Samuel
Such as: public static bool EqualsCaseInsensitive(this string a, string b) { return string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase); }
Samuel
If you want culture sensitive comparison, use this method. If you just want to make sure "FILE" and "file" are both accepted, use "OrdinalIgnoreCase" or your code might not work in places like Turkish locales. For more info, see http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html
Jeff Moser
Not sure what Samuel is talking about... this answer is perfect. Its correct and self-explanatory. It needs no references. +1
Sailing Judo
I just wonder... is this was answered in a two seconds?
Mendy
+7  A: 
System.Collections.CaseInsensitiveComparer

or

System.StringComparer.OrdinalIgnoreCase
leppie
Does this effect the entire application?
GateKiller
No, only when you use it.
leppie
Where can I find more info on this. Does this mean I can use == for a case insensitive match?
GateKiller
A: 

Operator? NO, but I think you can change your culture so that string comparison is not case-sensitive.

// you'll want to change this...
System.Threading.Thread.CurrentThread.CurrentCulture
// and you'll want to custimize this
System.Globalization.CultureInfo.CompareInfo

I'm confident that it will change the way that strings are being compared by the equals operator.

John Leidegren
That's a bit an ugly hack imho...
Frederik Gheysels
Yeah, to say the very least it's utterly not what you would want to do unless you want all string comparisons to be case insensitive. But I think it changes the behavior of the equals operator.
John Leidegren
+3  A: 
string.Equals(StringA, StringB, StringComparison.CurrentCultureIgnoreCase);
Erick
A: 
if (StringA.ToUpperInvariant() == StringB.ToUpperInvariant()) {

People report ToUpperInvariant() is faster than ToLowerInvariant().

knoopx
Invariant might be a bad idea if the current or desired culture has special rules for upper-casing.
OregonGhost
And the semantics are different too.
leppie
Does this create a new copy of each string? If so, bad idea.
ck
This will also throw an exception if either (or both) strings are null.
tvanfosson
Performance-wise, this is not such a good solution as you will create 2 new string instances here as well.
Frederik Gheysels
A: 

You can use

if (stringA.equals(StringB, StringComparison.CurrentCultureIgnoreCase))
Andy Mikula
+1  A: 

or

if (StringA.Equals(StringB, StringComparison.CurrentCultureIgnoreCase)) {

but you need to be sure that StringA is not null. So probably better tu use:

string.Equals(StringA , StringB, StringComparison.CurrentCultureIgnoreCase);

as John suggested

EDIT: corrected the bug

Grzenio
A: 
string.Compare(string1, string2, true)
This can have I18N problems.
Jay Bazuzi
+1  A: 

There are a number of properties on the StringComparer static class that return comparers for any type of case-sensitivity you might want:

StringComparer Properties

For instance, you can call

StringComparer.CurrentCultureIgnoreCase.Equals(string1, string2)

or

StringComparer.CurrentCultureIgnoreCase.Compare(string1, string2)

It's a bit cleaner than the string.Equals or string.Compare overloads that take a StringComparison argument.

Kyralessa
+1  A: 

System.StringComparer.OrdinalIgnoreCase

http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html

PoweRoy