C#:CompareTo(String) 和 Equals(String) 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9089716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:51:31  来源:igfitidea点击:

C#: What is the difference between CompareTo(String) and Equals(String)?

c#string

提问by Kit Ho

I would like to know, when comparing string in C#? which method is suitable to use and why?

我想知道,在 C# 中比较字符串时?哪种方法适合使用,为什么?

CompareTo()or Equals()?

CompareTo()或者Equals()

采纳答案by ken

From MSDN:

MSDN

string.CompareTo:

string.CompareTo

Compares this instance with a specified object or String and returns an integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or String.

将此实例与指定的对象或字符串进行比较,并返回一个整数,该整数指示此实例在排序顺序中是在指定对象或字符串的前面、后面还是出现在相同的位置。

string.Equals:

字符串.等于

Determines whether two String objects have the same value.

确定两个 String 对象是否具有相同的值。

In short, CompareTois used for sorting. Equalsis used to determine equality.

简而言之,CompareTo就是用来排序的。Equals用于确定相等性。

回答by Andrew Barber

CompareTo()tells you which, and if, one is greater/less than the other, while Equals()simply tells you if they are equivalent values.

CompareTo()告诉您哪个以及是否一个大于/小于另一个,而Equals()只是告诉您它们是否相等。

If all you want to know is "are they the same values", you use Equals(). If you need to also know how they compare, use CompareTo()

如果您只想知道“它们是否具有相同的值”,则使用Equals(). 如果您还需要知道它们的比较方式,请使用CompareTo()

int a = 50;
int b = 10;

//if you need to know if they are equal:
if(a.Equals(b)){
    //won't execute
}

//this would check if they are equal, as well
if(a.CompareTo(b) == 0){
    //won't execute
}

//if you need to know if a is bigger than b, specifically:
if(a.CompareTo(b) > 0){
    //will execute
}

//this would check to see if a is less than b
if(a.CompareTo(b) < 0){
    //won't execute
}

Finally, note that these Equals()and CompareTo()methods are not strictly needed for primitive types like int, because the standard comparison operators are overloaded, so you could do these:

最后,请注意,这些Equals()CompareTo()方法对于像 这样的原始类型并不是严格需要的int,因为标准比较运算符已重载,因此您可以执行以下操作:

//this would check if they are equal, as well
if(a == b){
    //won't execute
}

//if you need to know if a is bigger than b, specifically:
if(a > b){
    //will execute
}

//this would check to see if a is less than b
if(a < b){
    //won't execute
}

Finally, you mentioned stringin your question. Equals()and CompareTo()work as I have describe for stringas well. Just keep in mind the 'comparison' that CompareTo()does on strings is based on alphabetical sorting, so "abcdefg" < "z"

最后,您string在问题中提到了。Equals()CompareTo()按照我所描述的string那样工作。请记住,CompareTo()对字符串进行的“比较”是基于字母排序的,因此"abcdefg" < "z"

回答by ziesemer

Equalswill return a boolean for equality.

Equals将返回一个布尔值相等。

CompareTowill return an int, with -1 (or any other negative value) for "less than", 0 for "equals", or 1 (or any other positive value) for "greater than". This method is useful for sorting algorithms.

CompareTo将返回一个 int,“小于”为 -1(或任何其他负值),“等于”为 0,“大于”为 1(或任何其他正值)。这种方法对于排序算法很有用。

回答by JaredPar

The functionality in CompareTois actually a superset of functionality of Equals. A CompareTofunction dictates ordering, before, after or equals while the Equalsfunction merely dictates equality. Hence it's actually possible to define Equalsin terms of CompareTo

中的功能CompareTo实际上是 的功能的超集Equals。一个CompareTo功能使然排序,前或后相等,而Equals功能只是规定平等。因此实际上可以定义EqualsCompareTo

public bool Equals(string other) {
  return 0 == CompareTo(other);
}

回答by Thit Lwin Oo

CompareTo method is comparing instance of object with parameter of String object. Equals method determine the value of both are the same or not.

CompareTo 方法将对象的实例与 String 对象的参数进行比较。Equals 方法确定两者的值是否相同。

CompareTo should be used when you are comparing two objects' values.

当您比较两个对象的值时,应使用 CompareTo。

String str1 = "abc";
String str2 = "def"
if(strq.CompareTo(str2) //

Equals should be used when either one or both are not objects.

当一个或两个都不是对象时,应使用等于。

string str1 = "abc";
if(str1.Equals("abc") //

If you use CompareTo method for normal value type variables, it will use type cast (boxing) which is not necessary.

如果您对普通值类型变量使用 CompareTo 方法,它将使用不必要的类型转换(装箱)。

回答by Krishna

Equality can be “fussier” than comparison, but not vice versa. CompareTo can say “All objects are equal” while Equals says “But some are more equal than others!”

平等可能比比较更“挑剔”,但反之则不然。CompareTo 可以说“所有对象都是平等的”,而 Equals 可以说“但有些人比其他人更平等!”

An example of this is System.String. String's Equals method and == operator use ordinal comparison, which compares the Unicode point values of each character. Its CompareTo method, however, uses a less fussy culture-dependent comparison. On most computers, for instance, the strings “?” and “ǖ” are different according to Equals, but the same according to CompareTo.

这方面的一个例子是 System.String。String 的 Equals 方法和 == 运算符使用序数比较,它比较每个字符的 Unicode 点值。然而,它的 CompareTo 方法使用不那么繁琐的文化相关比较。例如,在大多数计算机上,字符串“?” 和“ǖ”根据 Equals 不同,但根据 CompareTo 相同。

This is from C# in a nutshell

简而言之,这是来自C#