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

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

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

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


当前回答

关于字符串比较问题的很好的解释和实践可以在文章中找到:在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类进行排序。

其他回答

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

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

s2 . compareto (s2):如果主要目的是确定两个字符串是否等效,则不要使用 s1 == s2:不能忽略大小写 s1。Equals(s2, StringComparison):如果s1为空,抛出NullReferenceException 字符串。Equals(s2, StringComparison):通过消除过程,这个静态方法是WINNER(假设一个典型的用例来确定两个字符串是否等效)!

从MSDN:

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

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

关于字符串比较问题的很好的解释和实践可以在文章中找到:在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类进行排序。

需要注意的一个重大区别是. equals()将在第一个字符串为空时抛出异常,而==则不会。

       string s = null;
        string a = "a";
        //Throws {"Object reference not set to an instance of an object."}
        if (s.Equals(a))
            Console.WriteLine("s is equal to a");
        //no Exception
        if(s==a)
            Console.WriteLine("s is equal to a");