Can C# CompareTo() and Compare() returns integers other than -1 and 1?
我想澄清三个问题:
- 这些方法返回的"相对"整数是否包括
-1
和1
以外的其他数字? - 在C#中,哪些字母在排序顺序中排在第一位:大写还是小写?
- 字母在C#中是否有一个固定的整数值,可以用来计算或交叉判断
CompareTo()
或Compare()
的返回值?
我的问题是基于这样一个事实:我访问过的每个网站都说C#CompareTo()
和Compare()
方法都会比较两个字符串并返回如下值:
- 其中
string1
来自beforestring2
,方法返回-1
; - 其中
string1
来自afterstring2
,方法返回1
;并且, - 如果
string1
和string2
相同,则方法返回0
.
如果我只保留网站上的例子,我会得到相同的结果.然而,当我使用我自己的字符串时,CompareTo()
和Compare()
,我得到不同的数字:不仅仅是-1
或1
,就像这个例子一样:
Console.WriteLine(String.Compare("Aloha!", "Hello!"));
Console.WriteLine(String.Compare("Hello!", "Aloha!"));
Console.WriteLine(String.Compare("Hello!", "Hello!"));
控制台输出
-7
7
0