字符串比较? [英] String Comparison?
本文介绍了字符串比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
的 string中的差异比较C#方法
是否有这些方法有什么区别?
Is there any difference between these methods?
string.Compare(s1, s2) == 0
s1.CompareTo(s2) == 0
s1.Equals(s2)
s1 == s2
$ b $ 2 b
我应该使用哪种
Which one should I use?
推荐答案
从反射:
public static int Compare(string strA, string strB)
{
return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.None);
}
public int CompareTo(string strB)
{
if (strB == null)
{
return 1;
}
return CultureInfo.CurrentCulture.CompareInfo.Compare(this, strB, CompareOptions.None);
}
所以的CompareTo
有额外的参考检查比比较
。
So CompareTo
has an additional reference check than Compare
.
public static bool operator ==(string a, string b)
{
return Equals(a, b);
}
所以 ==
是完全一样的等于
。
两个比较
和两个等于
是,你可以通过 CompareOptions之间的区别
参数比较
,并返回0/1 / -1。而等于
没有收到 CompareOptions
参数,它可以告诉你TRUE / FALSE仅
So ==
is exactly the same as Equals
.
The difference between two Compare
and two Equals
is, you can pass CompareOptions
argument to Compare
, and it returns 0/1/-1. while Equals
doesn't receive a CompareOptions
argument, and it can tell you TRUE/FALSE only.
这篇关于字符串比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文