Compare() 和 CompareTo() 在 C# 中用于字符串有什么区别?

huangapple go评论56阅读模式
英文:

What is the difference between Compare() and CompareTo() for strings in C#?

问题

我知道它返回整数,但我需要知道依据是什么。

示例:

string s1 = "world";
string s2 = "World";

Console.WriteLine(string.Compare(s1, s2));

Console.WriteLine(s1.CompareTo(s2));

它对两者都返回-1。但如果我们在s1中将w大写,而在s2中将w小写,它将返回1

因此,需要对此进行一些解释。

英文:

I know it returns integer but I needed to know on what basis.

Example:

string s1 = "world";
string s2 = "World";

Console.WriteLine(string.Compare(s1,s2));   

Console.WriteLine(s1.CompareTo(s2));

it returns -1 for both. But if we capitalize w in s1 and lowercase w in s2 it returns 1.

So need some explanation for it.

答案1

得分: 3

我们可以看到在一个(或两个)字符串为null时的差异

string s1 = null;
string s2 = "World";

// -1,因为 null < "World"
Console.WriteLine(string.Compare(s1, s2));   

// 抛出 NullReferenceException,尝试调用 null.CompareTo("World")
Console.WriteLine(s1.CompareTo(s2));
英文:

We can see the difference when one (or both strings) is null:

string s1 = null;
string s2 = &quot;World&quot;;

// -1, since null &lt; &quot;World&quot;
Console.WriteLine(string.Compare(s1,s2));   

// NullReferenceException thrown, attempt to call null.CompareTo(&quot;World&quot;)
Console.WriteLine(s1.CompareTo(s2));

答案2

得分: 3

如果我们讨论行为,那么它们是相同的,并且在底层都调用相同的 static int string.Compare,并使用默认的 StringComparison.CurrentCulture

主要区别在于 static methodinstance method。因此,如果 s1 为空,s1.CompareTo(s2) 会引发 NullReferenceException

英文:

If we are talking about behaviour then they are the same and both under the hood call same static int string.Compare with defaulted StringComparison.CurrentCulture.

The main difference lies in static method vs instance method. So s1.CompareTo(s2) throw NullReferenceException if s1 is null.

答案3

得分: 1

在C#中,Compare()CompareTo() 方法都用于字符串比较,但它们具有不同的实现和返回类型。

  1. Compare() 方法:
  • Compare() 方法是C#中 string 类的静态方法。它比较两个字符串并返回一个整数,表示它们在排序方面的相对顺序。
  • Compare() 的返回值是一个整数,如果第一个字符串小于第二个字符串,则返回小于0的整数,如果它们相等,则返回0,如果第一个字符串大于第二个字符串,则返回大于0的整数。
  • 它允许您基于指定的 StringComparison 枚举值执行区分大小写或不区分大小写的比较。
  • Compare() 方法可用于排序和排序操作。

Compare() 的示例用法:

string string1 = "apple";
string string2 = "banana";
int result = string.Compare(string1, string2);
  1. CompareTo() 方法:
  • CompareTo() 方法是C#中 string 类的实例方法。
  • 它将当前字符串实例与另一个字符串进行比较,并返回一个整数,表示它们在排序方面的相对顺序。
  • CompareTo() 的返回值与 Compare() 类似:如果当前字符串小于另一个字符串,则返回小于0的值,如果它们相等,则返回0,如果当前字符串大于另一个字符串,则返回大于0的值。
  • 默认情况下,它执行区分大小写的比较,但可以与 StringComparison 枚举一起使用以指定不区分大小写的比较。
  • CompareTo() 方法通常用于自然排序或在面向对象的方式比较字符串。

CompareTo() 的示例用法:

string string1 = "apple";
string string2 = "banana";
int result = string1.CompareTo(string2);

Compare()CompareTo() 都可用于比较字符串,但 Compare() 是一个静态方法,接受两个字符串参数,而 CompareTo() 是一个实例方法,在单个字符串实例上调用。

英文:

In C#, both the Compare() and CompareTo() methods are used for string comparisons, but they have different implementations and return types.

  1. Compare() Method:
  • The Compare() method is a static method of the string class in C#. It
    compares two strings and returns an integer that indicates their
    relative order in terms of sorting.
  • The return value of Compare() is an integer less than 0 if the first
    string is less than the second, 0 if they are equal, or an integer
    greater than 0 if the first string is greater than the second.
  • It allows you to perform case-sensitive or case-insensitive
    comparisons based on the specified StringComparison enumeration
    value.
  • The Compare() method can be useful for sorting and ordering
    operations.

Example usage of Compare():

string string1 = &quot;apple&quot;;
string string2 = &quot;banana&quot;;
int result = string.Compare(string1, string2);
  1. CompareTo() Method:
  • The CompareTo() method is an instance method of the string class in
    C#.
  • It compares the current string instance with another string and
    returns an integer that indicates their relative order in terms of
    sorting.
  • The return value of CompareTo() is similar to Compare(): less than 0
    if the current string is less than the other, 0 if they are equal, or
    greater than 0 if the current string is greater.
  • It performs a case-sensitive comparison by default but can be used
    with the StringComparison enumeration to specify case-insensitive
    comparisons.
  • The CompareTo() method is typically used for natural sorting or when
    comparing strings in an object-oriented manner.

Example usage of CompareTo():

string string1 = &quot;apple&quot;;
string string2 = &quot;banana&quot;;
int result = string1.CompareTo(string2); 

Both Compare() and CompareTo() can be used to compare strings, but Compare() is a static method that accepts two string parameters, while CompareTo() is an instance method called on a single string instance.

huangapple
  • 本文由 发表于 2023年5月17日 16:17:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定