What is difference between different string compare methods [duplicate]

浪尽此生 提交于 2019-11-28 05:17:28

Ripped from msdn

string.Equals

Determines whether this instance and a specified object, which must also be a String object, have the same value.

string.Compare Compares two specified String objects and returns an integer that indicates their relative position in the sort order.

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.CompareOrdinal Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.

String equality operators The predefined string equality operators are:

bool operator ==(string x, string y); bool operator !=(string x, string y); Two string values are considered equal when one of the following is true:

Both values are null. Both values are non-null references to string instances that have identical lengths and identical characters in each character position. The string equality operators compare string values rather than string references. When two separate string instances contain the exact same sequence of characters, the values of the strings are equal, but the references are different. As described in Section 7.9.6, the reference type equality operators can be used to compare string references instead of string values.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!