3

Possible Duplicate:
Is there a C# case insensitive equals operator?

string string1 = "aBc"

string string2 = "AbC"

how can I check if string1 is equal to string2 and have it return true, regardless of case sensitivity.

Community
  • 1
  • 1
ArcanaForce0
  • 55
  • 1
  • 4
  • 4
    Duplicate of: http://stackoverflow.com/questions/631233/is-there-a-c-case-insensitive-equals-operator – Jed Smith Sep 07 '09 at 01:03

5 Answers5

10

Two approaches:

You can .ToLower() and do string-equality, or you can use this:

string.Equals(string1, string2, StringComparison.CurrentCultureIgnoreCase)

Edit: To appease the downvoters, this operation is useful if your data is culturally significant (i.e., you're comparing Scandinavian words and your current locale is set correctly). If this data is culturally agnostic, and you don't care about locales (bad idea, particularly since .NET lives for Unicode), you can do this:

string.Equals(string1, string2, StringComparison.OrdinalIgnoreCase)
Jed Smith
  • 15,584
  • 8
  • 52
  • 59
3

You should use the recommendations here MSDN: "Recommendations for String Use" :

  • DO: Use StringComparison.Ordinal or OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.
  • DO: Use StringComparison.Ordinal and OrdinalIgnoreCase comparisons for increased speed.
  • DO: Use StringComparison.CurrentCulture-based string operations when displaying the output to the user.
  • DO: Switch current use of string operations based on the invariant culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is linguistically irrelevant (symbolic, for example).
  • DO: Use ToUpperInvariant rather than ToLowerInvariant when normalizing strings for comparison.
  • DON'T: Use overloads for string operations that don't explicitly or implicitly specify the string comparison mechanism.
  • DON'T: Use StringComparison.InvariantCulture-based string operations in most cases; one of the few exceptions would be persisting linguistically meaningful but culturally-agnostic data.

I must admit they were an eyeopener for me. Especially the last one.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • The OP did not specify whether this was culturally agnostic. Scandinavian locales, specifically, have unique rules on what characters are capitals of others. Thanks for the down vote, though. – Jed Smith Sep 07 '09 at 01:11
2

You can also use string.Compare, adding the third parameter, which is ignoreCase:

if (string.Compare(string1, string2, true) == 0) 
{ 
   // string are equal
}

And you could use also the CompareInfo class:

if (CultureInfo.CurrentCulture.CompareInfo.Compare(string1, string2, 
    CompareOptions.IgnoreCase) == 0)
{
   // string are equal
}
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1
string.Equals(string1, string2, StringComparison.CurrentCultureIgnoreCase);

:D

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
1
string.Equals("aBc", "AbC", StringComparison.CurrentCultureIgnoreCase) 
Kamil Szot
  • 17,436
  • 6
  • 62
  • 65