3

Possible Duplicate:
Which is generally best to use — StringComparison.OrdinalIgnoreCase or StringComparison.InvariantCultureIgnoreCase?

When using the String.Equals(string a, string b, StringComparison comparisonType) and not caring about the case of 'a' and 'b', what's the proper StringComparison to use? In other words, what's the difference between StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase, and StringComparison.OrdinalIgnoreCase, and how will each one affect the use of String.Equals?

Community
  • 1
  • 1
Donut
  • 110,061
  • 20
  • 134
  • 146
  • Which is the "proper" one depends on what use you're putting the string comparisons to. So, what are you trying to do? WHY are you comparing two strings for case-insensitive equality? Where did the strings come from, and where is the result going? – Eric Lippert Jun 11 '09 at 20:42
  • Looks similar: http://stackoverflow.com/questions/72696/which-is-generally-best-to-use-stringcomparison-ordinalignorecase-or-stringcom – Greg Jun 11 '09 at 20:43
  • This [MSDN](http://msdn.microsoft.com/en-us/library/system.stringcomparison.aspx) page gives the details, but no examples. – ChrisF Jun 11 '09 at 20:18

1 Answers1

4

The thing is that uppercase versions of lowercase letters are not same in all languages. For example, uppercase of "u" is "U" in en-us, but it may be something else in some other culture.

Try this, for example:

        CultureInfo english = new CultureInfo("en-US", false);
        CultureInfo turkish = new CultureInfo("tr-TR", false);

        foreach (String i in new String[] { "a", "e", "i", "o", "u" })
        {
            String iEng = i.ToUpper(english);
            String iTur = i.ToUpper(turkish);
            Console.Write(i); 
            Console.WriteLine(iEng == iTur ? " Equal" : " Not equal");
        }

        Console.ReadLine();

So if you have strings entered in current thread's culture, containing such letters, using invariant culture might give wrong results. So I would use CurrentCultureIgnoreCase, if you are sure that the compared strings were entered in current culture and should be compared that way also.

vgru
  • 49,838
  • 16
  • 120
  • 201
  • Nice example. You can give link to this online demonstation : http://www.ideone.com/6kqy3 – Nawaz Feb 03 '11 at 06:41