在c#中比较字符串是非常简单的。事实上有几种方法可以做到这一点。我在下面列出了一些。我好奇的是它们之间的区别,以及什么时候应该使用其中一种?是否应该不惜一切代价避免?还有其他我没有列出的吗?

string testString = "Test";
string anotherString = "Another";

if (testString.CompareTo(anotherString) == 0) {}
if (testString.Equals(anotherString)) {}
if (testString == anotherString) {}

(注:我在这个例子中寻找平等,不小于或大于,但也可以自由评论)


当前回答

使用. equals,你还可以获得StringComparison选项。对于忽略大小写和其他东西非常方便。

顺便说一句,这将计算为假

string a = "myString";
string b = "myString";

return a==b

因为==比较a和b(它们是指针)的值,只有当指针指向内存中的同一个对象时,这个函数才会求值为true。. equals解引用指针并比较指针上存储的值。 a = (b)在这里为真。

如果把b改成:

b = "MYSTRING";

那么a = (b)是假的,但是

a.Equals(b, StringComparison.OrdinalIgnoreCase) 

是正确的

a.CompareTo(b) calls the string's CompareTo function which compares the values at the pointers and returns <0 if the value stored at a is less than the value stored at b, returns 0 if a.Equals(b) is true, and >0 otherwise. However, this is case sensitive, I think there are possibly options for CompareTo to ignore case and such, but don't have time to look now. As others have already stated, this would be done for sorting. Comparing for equality in this manner would result in unecessary overhead.

我确定我遗漏了一些东西,但我认为如果你需要更多细节,这些信息应该足够开始试验了。

其他回答

关于字符串比较问题的很好的解释和实践可以在文章中找到:在Microsoft . net 2.0中使用字符串的新建议和在. net框架中使用字符串的最佳实践。


上述每一种方法(或其他方法)都有特定的目的。它们之间的关键区别是默认情况下使用哪种类型的StringComparison Enumeration。有以下几种选择:

CurrentCulture CurrentCultureIgnoreCase InvariantCulture InvariantCultureIgnoreCase 序数 OrdinalIgnoreCase

以上每一种比较类型都针对不同的用例:

Ordinal Case-sensitive internal identifiers Case-sensitive identifiers in standards like XML and HTTP Case-sensitive security-related settings OrdinalIgnoreCase Case-insensitive internal identifiers Case-insensitive identifiers in standards like XML and HTTP File paths (on Microsoft Windows) Registry keys/values Environment variables Resource identifiers (handle names, for example) Case insensitive security related settings InvariantCulture or InvariantCultureIgnoreCase Some persisted linguistically-relevant data Display of linguistic data requiring a fixed sort order CurrentCulture or CurrentCultureIgnoreCase Data displayed to the user Most user input

注意,StringComparison枚举和字符串比较方法的重载从。net 2.0开始就存在了。


字符串。CompareTo方法

实际上是IComparable的类型安全实现。CompareTo方法。默认解释:CurrentCulture。

用法:

CompareTo方法主要用于排序或字母排序操作

Thus

实现IComparable接口必须使用此方法

字符串。比较的方法

String类的静态成员,有很多重载。默认解释:CurrentCulture。

只要可能,应该调用包含StringComparison参数的Compare方法的重载。

字符串。=方法

从Object类重写,并为类型安全重载。默认解释:序数。 注意:

String类的相等方法包括静态Equals、静态操作符==和实例方法Equals。


StringComparer 类

还有另一种处理字符串比较的方法,尤其是用于排序的方法:

可以使用StringComparer类创建特定于类型的比较,以便对泛型集合中的元素进行排序。Hashtable、Dictionary、SortedList和SortedList等类使用StringComparer类进行排序。

从MSDN:

CompareTo方法主要用于排序或 排序操作。它不应该用于初级 该方法调用的目的是确定两个字符串是否正确 等价的。要确定两个字符串是否等效,请调用 Equals方法。”

他们建议在仅寻找相等时使用. equals而不是. compareto。我不确定字符串类的. equals和==之间是否有区别。我有时会使用。equals或Object。在我自己的类中使用ReferenceEquals而不是==,以防稍后有人出现并为该类重新定义==操作符。

在你列出的表格中,两者之间没有太大的区别。CompareTo最终调用CompareInfo方法,该方法使用当前区域性进行比较;Equals由==运算符调用。

如果考虑到过载,情况就不一样了。Compare和==只能使用当前区域性来比较字符串。等号和字符串。Compare可以接受一个StringComparison枚举参数,允许您指定区域性不敏感或大小写不敏感的比较。只有字符串。Compare允许您指定CultureInfo并使用默认区域性以外的区域性执行比较。

由于它的多功能性,我发现我使用了String。比任何其他比较方法都要比较;它可以让我精确地指定我想要的东西。

使用. equals也更容易阅读。

不是性能通常有99%的时间你需要这样做,但是如果你不得不这样做在一个循环中数百万倍。我强烈建议你使用.Equals或= =因为当它发现一个字符不匹配它把整个事情是假的,但是如果你使用CompareTo就必须找出哪些字符小于另一个,从而导致稍差的性能。

如果您的应用程序将在不同的国家运行,我建议您查看CultureInfo的含义,并可能使用. equals。因为我只为美国编写应用程序(并不关心它是否由某人正常工作),我总是使用==。