gpt4 book ai didi

c# - StringComparison.InvariantCulture 澄清?

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:19 24 4
gpt4 key购买 nike

根据 MSDN:StringComparison.InvariantCulture:

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

好吧,我没有在我的样本中进行任何排序,而且仍然不明白为什么它会产生它产生的结果:

/*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 使用基于英语 的比较规则,但没有任何地区差异

1) 那么为什么它说 lasst 等于 laßt ? (一个连英文字符都没有...)

2) 为什么(如果将其扁平化为英文)hello 不等于 héllo

最佳答案

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.

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

关于c# - StringComparison.InvariantCulture 澄清?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17945119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com