.NET 中的字符串比较

发布于 2025-01-06 10:16:36 字数 161 浏览 1 评论 0原文

(.NET) 之间有什么区别(以 bref 表示)

myString == otherString

(.NET)和

myString.CompareTo(otherString) == 0

What is the difference (in bref) between (.NET)

myString == otherString

and

myString.CompareTo(otherString) == 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你的呼吸 2025-01-13 10:16:36

没有什么区别,除了当 myString 为 null 时,在这种情况下 myString.CompareTo(otherString) 会抛出错误 (NullReferenceException)。另外,使用 CompareTo== 慢一点。

仅当您有兴趣了解某个字符串按字母顺序排序时是否位于另一个字符串之前或之后时,才使用 CompareTo。例如,"Car".CompareTo("Cat") 返回 -1,因为按字母顺序排序时,“Car”位于“Cat”之前。

There's no difference, except when myString is null, in which case myString.CompareTo(otherString) throws an error (NullReferenceException). Also, using CompareTo is a little bit slower than ==.

Only use CompareTo when you are interested in knowing if a string is before or after another one in an alphabetical sorting of them. For example "Car".CompareTo("Cat") returns -1 because "Car" is before "Cat" when ordered alphabetically.

高冷爸爸 2025-01-13 10:16:36

CompareTo 应该用于评估排序。无论出于何种原因,两个字符串出于排序目的进行比较时可能相同,但不应被视为相等(即 ==Equals 可能返回 假)。

引用文档

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

添加了强调 - 请注意,它没有说这两个对象是相等的。

CompareTo should only be used for assessing ordering. It may be that, for whatever reason, two strings compare the same for ordering purposes, but should not be considered equal (that is, == and Equals may return false).

To quote the documentation:

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.

Emphasis added - note that it does not say that the two objects are equal.

流心雨 2025-01-13 10:16:36

假设你的意思

myString == otherString

是没有明显的差异。

Assuming that you meant

myString == otherString

there is no visible difference.

不忘初心 2025-01-13 10:16:36

假设您的意思是 == 而不是 =

CompareTo 实现了 IComparable 接口。它返回一个整数。

Assuming you meant == and not =

CompareTo implements the IComparable interface. It returns an integer.

§普罗旺斯的薰衣草 2025-01-13 10:16:36

来自此处

CompareTo 方法主要用于排序或
按字母顺序排列的操作。当主要
该方法调用的目的是判断两个字符串是否是
相等的。要确定两个字符串是否相等,请调用
等于方法。

Equals 方法更合适。从此处开始,Equals==< 之间的区别/code> 的特点是 Equals 要求其参数非空,而 == 则不需要。另外,== 被实现为使用 Equals,因此 Equals 将始终具有更好的性能。

From here:

The CompareTo method was designed primarily for use in sorting or
alphabetizing operations. It should not be used when the primary
purpose of the method call is to determine whether two strings are
equivalent. To determine whether two strings are equivalent, call the
Equals method.

The Equals method is more appropriate. From here, the difference between Equals and == is that Equals requires its parameter to be non-null and == does not. Plus, == is implemented to use Equals so Equals will always have better performance.

标点 2025-01-13 10:16:36

myString.CompareTo(otherString) 方法的主要目的是用于排序或按字母顺序排列
运营。当主要目的是检查字符串的相等性时,不应使用它。

要确定两个字符串是否相等,请调用 Equals 方法。”

当仅查找相等性时,最好使用 .Equals 而不是 .CompareTo。因为我也认为编译器比 == 操作更快。

The myString.CompareTo(otherString) method main purpose is to be used with sorting or alphabetizing
operations. It should not be used when the main purpose is to check the equality of strings.

To determine whether two strings are equivalent, call the Equals method."

It's better to use .Equals instead of .CompareTo when looking solely for equality. since I also think it is faster for the compiler than the == operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文