C# compareto 方法如何比较字符串
How C# compareto method compares Strings
我想知道C#的CompareTo方法是如何比较两个字符串的,所以我这样测试了:
string str1 = "0";
string str2 = "-";
Console.WriteLine(str1.CompareTo(str2)); // output : 1
string str3 = "01";
string str4 = "-1";
Console.WriteLine(str3.CompareTo(str4)); // output : -1
为什么结果不同?
TLDR:默认的字典字符串排序会特殊处理 -
个字符。
答案是默认的字符串比较使用字典排序规则。
这意味着一些符号 - 例如,-
,被特殊处理。
The documentation for CompareOptions 状态:
The .NET Framework uses three distinct ways of sorting: word sort,
string sort, and ordinal sort. Word sort performs a culture-sensitive
comparison of strings. Certain nonalphanumeric characters might have
special weights assigned to them. For example, the hyphen ("-") might
have a very small weight assigned to it so that "coop" and "co-op"
appear next to each other in a sorted list. String sort is similar to
word sort, except that there are no special cases. Therefore, all
nonalphanumeric symbols come before all alphanumeric characters.
Ordinal sort compares strings based on the Unicode values of each
element of the string.
在您的例子中,使用的是默认排序:单词排序。
您可以通过在 string.Compare()
中指定您想要的比较类型来查看不同的结果:
string str3 = "01";
string str4 = "-1";
Console.WriteLine(Math.Sign(string.Compare(str3, str4, StringComparison.InvariantCulture))); // output : -1
Console.WriteLine(Math.Sign(string.Compare(str3, str4, StringComparison.Ordinal))); // output : 1
在这里你可以看到它在不进行序数比较时特别对待 -
。
确实是 -
被特殊对待 - 它不假设它是减号。例如,如果您使用 +
而不是 -
,您将得到:
string str1 = "0";
string str2 = "+";
Console.WriteLine(Math.Sign(string.Compare(str1, str2, StringComparison.InvariantCulture))); // output : 1
Console.WriteLine(Math.Sign(string.Compare(str1, str2, StringComparison.Ordinal))); // output : 1
string str3 = "01";
string str4 = "+1";
Console.WriteLine(Math.Sign(string.Compare(str3, str4, StringComparison.InvariantCulture))); // output : 1
Console.WriteLine(Math.Sign(string.Compare(str3, str4, StringComparison.Ordinal))); // output : 1
一边
不要将普通连字符与软连字符混淆!
- 普通连字符具有 Unicode 值
\u002D
。
- 软连字符具有 Unicode 值
\u00AD
。
注意 the documentation for string.Compare()
其中的示例代码显示 soft 连字符被忽略。文档指出:
Character sets include ignorable characters. The
Compare(String, String, Boolean) method does not consider such
characters when it performs a culture-sensitive comparison.
A soft 连字符是可忽略字符之一,但重要的是要注意软连字符与普通连字符不同。因此本文档不适用于您的示例代码。
正常 连字符表现不同的实际原因已在上面给出。
(如果您想要 Unicode 中所有可忽略字符的完整列表,请转至 http://www.unicode.org/Public/UNIDATA/DerivedCoreProperties.txt 并搜索 Default_Ignorable_Code_Point
- 请注意,该列表实际上不包括普通连字符。)
我想知道C#的CompareTo方法是如何比较两个字符串的,所以我这样测试了:
string str1 = "0";
string str2 = "-";
Console.WriteLine(str1.CompareTo(str2)); // output : 1
string str3 = "01";
string str4 = "-1";
Console.WriteLine(str3.CompareTo(str4)); // output : -1
为什么结果不同?
TLDR:默认的字典字符串排序会特殊处理 -
个字符。
答案是默认的字符串比较使用字典排序规则。
这意味着一些符号 - 例如,-
,被特殊处理。
The documentation for CompareOptions 状态:
The .NET Framework uses three distinct ways of sorting: word sort, string sort, and ordinal sort. Word sort performs a culture-sensitive comparison of strings. Certain nonalphanumeric characters might have special weights assigned to them. For example, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list. String sort is similar to word sort, except that there are no special cases. Therefore, all nonalphanumeric symbols come before all alphanumeric characters. Ordinal sort compares strings based on the Unicode values of each element of the string.
在您的例子中,使用的是默认排序:单词排序。
您可以通过在 string.Compare()
中指定您想要的比较类型来查看不同的结果:
string str3 = "01";
string str4 = "-1";
Console.WriteLine(Math.Sign(string.Compare(str3, str4, StringComparison.InvariantCulture))); // output : -1
Console.WriteLine(Math.Sign(string.Compare(str3, str4, StringComparison.Ordinal))); // output : 1
在这里你可以看到它在不进行序数比较时特别对待 -
。
确实是 -
被特殊对待 - 它不假设它是减号。例如,如果您使用 +
而不是 -
,您将得到:
string str1 = "0";
string str2 = "+";
Console.WriteLine(Math.Sign(string.Compare(str1, str2, StringComparison.InvariantCulture))); // output : 1
Console.WriteLine(Math.Sign(string.Compare(str1, str2, StringComparison.Ordinal))); // output : 1
string str3 = "01";
string str4 = "+1";
Console.WriteLine(Math.Sign(string.Compare(str3, str4, StringComparison.InvariantCulture))); // output : 1
Console.WriteLine(Math.Sign(string.Compare(str3, str4, StringComparison.Ordinal))); // output : 1
一边
不要将普通连字符与软连字符混淆!
- 普通连字符具有 Unicode 值
\u002D
。 - 软连字符具有 Unicode 值
\u00AD
。
注意 the documentation for string.Compare()
其中的示例代码显示 soft 连字符被忽略。文档指出:
Character sets include ignorable characters. The Compare(String, String, Boolean) method does not consider such characters when it performs a culture-sensitive comparison.
A soft 连字符是可忽略字符之一,但重要的是要注意软连字符与普通连字符不同。因此本文档不适用于您的示例代码。
正常 连字符表现不同的实际原因已在上面给出。
(如果您想要 Unicode 中所有可忽略字符的完整列表,请转至 http://www.unicode.org/Public/UNIDATA/DerivedCoreProperties.txt 并搜索 Default_Ignorable_Code_Point
- 请注意,该列表实际上不包括普通连字符。)