s2 是规范化的 s1
,因为字符串 s1 和 s2 看起来相同
s1 和 s2 具有不同的 GetHashCode
字符串。比较显示 s1 和 s2 等效
s2 作为字符串有重音
s2.ToCharArray 删除重音
为什么 s2.ToCharArray 不同于 s2 作为字符串?
我想到了。
s2 的长度是 4。
重音只是作为一个单独的字符(Int16 = 769)被剥离。
String.Compare 足够聪明地弄清楚。
有趣的是 String.Compare 可以计算出来,但 String.Contains 没有。
string s1 = "xxé";
string s1copy = "xxé";
string s2 = s1.Normalize(NormalizationForm.FormD);
string s2b = "xxe";
char accent = 'é';
Debug.WriteLine(s1); // xxé
Debug.WriteLine(s2); // xxé
Debug.WriteLine(s2b); // xxe
Debug.WriteLine(s1.GetHashCode()); // 424384421
Debug.WriteLine(s1copy.GetHashCode()); // 424384421
Debug.WriteLine(s2.GetHashCode()); // 1057341801
Debug.WriteLine(s2b.GetHashCode()); // 1701495145
Debug.WriteLine(s1.Contains(accent)); // true
Debug.WriteLine(s2.Contains(accent)); // false
Debug.WriteLine(s2b.Contains(accent)); // false
Debug.WriteLine(string.Compare(s1, s1copy).ToString()); // 0
Debug.WriteLine(string.Compare(s1, s2).ToString()); // 0
Debug.WriteLine(string.Compare(s1, s2b).ToString()); // 1
Debug.WriteLine(string.Compare(s2, s2b).ToString()); // 1
Debug.WriteLine(s1.Equals(s1copy)); // true
Debug.WriteLine(s1.Equals(s2)); // false
Debug.WriteLine(s1.Equals(s2b)); // false
Debug.WriteLine(s2.Equals(s2b)); // false
Debug.WriteLine(s1 == s1copy); // true
Debug.WriteLine(s1 == s2); // false
Debug.WriteLine(s1 == s2b); // false
Debug.WriteLine(s2 == s2b); // false
char[] chars1 = s1.ToCharArray();
char[] chars2 = s2.ToCharArray();
char[] chars2b = s2b.ToCharArray();
Debug.WriteLine(chars1.Length.ToString()); // 3
Debug.WriteLine(chars2.Length.ToString()); // 4
Debug.WriteLine(chars2b.Length.ToString()); // 3
Debug.WriteLine(chars1[0].ToString() + " " + ((Int16)chars1[0]).ToString() + " " + chars1[1].ToString() + " " + ((Int16)chars1[1]).ToString() + " " + chars1[2].ToString() + " " + ((Int16)chars1[2]).ToString());
// x 120 x 120 é 233
Debug.WriteLine(chars2[0].ToString() + " " + ((Int16)chars2[0]).ToString() + " " + chars2[1].ToString() + " " + ((Int16)chars2[1]).ToString() + " " + chars2[2].ToString() + " " + ((Int16)chars2[2]).ToString() +" " + chars2[3].ToString() + " " + ((Int16)chars2[3]).ToString());
//x 120 x 120 e 101 ́ 769
Debug.WriteLine(chars2b[0].ToString() + " " + ((Int16)chars2b[0]).ToString() + " " + chars2b[1].ToString() + " " + ((Int16)chars2b[1]).ToString() + " " + chars2b[2].ToString() + " " + ((Int16)chars2b[2]).ToString());
//x 120 x 120 e 101
Debug.WriteLine(chars1.GetHashCode()); // 16098066
Debug.WriteLine(chars2.GetHashCode()); // 53324351
Debug.WriteLine(chars2b.GetHashCode()); // 50785559
Debug.WriteLine(chars1 == chars2); // false
Debug.WriteLine(chars1 == chars2b); // false
Debug.WriteLine(chars2 == chars2b); // false