Question

According To MSDN : StringComparison.InvariantCulture :

Specifies the culture, case, and sort rules to be used by certain overloads of the String.Compare and String.Equals methods.

Well , I'm not doing any sort here in my sample , And still don't understand why does it yield the result it yield :

/*1*/   void Main()
/*2*/   {
/*3*/        string s1 = "lasst";
/*4*/        string s2 = "laßt"; 
/*5*/        Console.WriteLine (s1.Equals(s2, StringComparison.InvariantCulture));  
/*6*/        //True
/*7*/        
/*8*/        
/*9*/        
/*10*/       string s3 = "hello";
/*11*/       string s4 = "héllo"; 
/*12*/       Console.WriteLine (s3.Equals(s4, StringComparison.InvariantCulture)); 
/*13*/       //False
/*14*/   }

InvariantCulture uses comparison rules based on english, but without any regional variations

1) So why it says that lasst is equal to laßt ? (one doesnt even has an english char...)

2) And why ( if it's flatten to english)hello is not equal to héllo ?

Was it helpful?

Solution

Relevant snippet from the book CLR via C#

Note When the Compare method is not performing an ordinal comparison, it performs character expansions. A character expansion is when a character is expanded to multiple characters regardless of culture. In the above case, the German Eszet character ‘ß’ is always expanded to ‘ss.’ Similarly, the ‘Æ’ ligature character is always expanded to ‘AE.’ So in the code example, the second call to Compare will always return 0 regardless of which culture I actually pass in to it.

You string "héllo" does not get transformed by character expansion internally, and so is not considered equal to "hello".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top