问题描述
当我执行下面一行时
returnVal=string.Compare("stringOne","stringTwo",StringComparison.OrdinalIgnoreCase);
我得到returnVal为-5。 谁能解释一下为什么会这样?还有其他字符串值,我得到的返回值为13,15等。 通常应为-1,1。如果我没看错。
解决方法
“通常应为-1,1。如果我没记错的话”
您是正确的,即使在Compare
的文档中,它们通常是IComparer.Compare
方法的返回值和推荐的做法:
https://docs.microsoft.com/en-us/troubleshoot/dotnet/csharp/use-icomparable-icomparer
“
IComparer.Compare
方法需要三级比较。根据一个值是否大于等于等于,返回1
,0
或-1
,或比其他方法少。可以通过在此方法中切换逻辑运算符来更改排序顺序(升序或降序)。“
但是,这没有在任何地方强制执行。
此外,String
类未实现IComparer<string>
,并且在该接口中也未定义Compare
签名的重载。
如果我们查看Compare
方法here的源代码,就会发现对于OrdinalIgnoreCase
,它调用了CompareOrdinalIgnoreCaseHelper
方法,该方法返回第一对不匹配字符之间的ascii值差异(大写之后)。如果字符串的长度不同,但是较长的首字符都与较短的首字符匹配,则返回其长度的差异。
private unsafe static int CompareOrdinalIgnoreCaseHelper(String strA,String strB)
{
Contract.Requires(strA != null);
Contract.Requires(strB != null);
Contract.EndContractBlock();
int length = Math.Min(strA.Length,strB.Length);
fixed (char* ap = &strA.m_firstChar) fixed (char* bp = &strB.m_firstChar)
{
char* a = ap;
char* b = bp;
while (length != 0)
{
int charA = *a;
int charB = *b;
Contract.Assert((charA | charB) <= 0x7F,"strings have to be ASCII");
// uppercase both chars - notice that we need just one compare per char
if ((uint)(charA - 'a') <= (uint)('z' - 'a')) charA -= 0x20;
if ((uint)(charB - 'a') <= (uint)('z' - 'a')) charB -= 0x20;
//Return the (case-insensitive) difference between them.
if (charA != charB)
return charA - charB;
// Next char
a++; b++;
length--;
}
return strA.Length - strB.Length;
}
}
因此,我们应该始终将Compare
方法的结果与0
而不是-1
或1
进行比较。