C#:是什么的CompareTo(字符串)和equals(字符串)之间的区别? [英] C#: What is the difference between CompareTo(String) and Equals(String)?

查看:825
本文介绍了C#:是什么的CompareTo(字符串)和equals(字符串)之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

的CompareTo()等于()

推荐答案

从的 MSDN

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

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

Determines whether two String objects have the same value.

在短,的CompareTo 被用于排序。 等于用于确定平等。

In short, CompareTo is used for sorting. Equals is used to determine equality.

这篇关于C#:是什么的CompareTo(字符串)和equals(字符串)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆