1

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 ?

4

1 回答 1

5

通过 C# 来自书籍 CLR 的相关片段

注意当 Compare 方法不执行序号比较时,它会执行字符扩展。字符扩展是指将一个字符扩展为多个字符,而不考虑文化。在上述情况下,德语 Eszet 字符“ß”始终扩展为“ss”。同样,“Æ”连字字符始终扩展为“AE”。因此,在代码示例中,第二次调用 Compare 将始终返回 0,而不管我实际传递给它的是哪种文化。

您的字符串“hello”不会在内部通过字符扩展进行转换,因此不被视为等于“hello”。

于 2013-07-30T11:06:11.293 回答