什么是不同的字符串比较方法之间的区别 [英] What is difference between different string compare methods

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

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c">Differences在字符串比较C#方法

Possible Duplicate:
Differences in string compare methods in C#

在.NET中有很多字符串比较的方法,我只是想确认哪一个是最好的 使用,并且也应该有最佳的性能。

In .NET there are many string comparison methods, i just wanna confirm which one is the best to use and also should have best performance.

string.Equals()

string.Compare()

string.CompareTo()

string.CompareOrdinal()

string.ReferenceEquals()

if (str1 == str2)

在此先感谢。

Thanks in advance.

推荐答案

从MSDN撕开

<一个href="http://msdn.microsoft.com/en-us/library/system.string.equals.aspx">string.Equals

确定此实例与指定的对象,这也必须是一个String对象,是否具有相同的值。

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

<一个href="http://msdn.microsoft.com/en-us/library/system.string.compare.aspx">string.Compare 比较两个指定的String对象,并返回一个整数,指示其在排序顺序相对位置。

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

<一个href="http://msdn.microsoft.com/en-us/library/system.string.compareto.aspx">string.CompareTo 比较此实例与指定的对象或字符串,并返回一个整数,指示是否该实例precedes,如下,或将出现在排序顺序为指定对象或字符串相同的位置。

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 每个字符串评估相应的字符对象的数值比较两个指定的String对象。

string.CompareOrdinal Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.

字符串相等运算符 在predefined字符串相等操作符是:

String equality operators The predefined string equality operators are:

布尔运算符==(字符串x,y字符串); 布尔运算符=(字符串x,y字符串)!; 两个字符串值被视为相等时,下列条件之一为真:

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:

这两个值都为null。 这两个值都是非空引用具有相同的长度和相同的字符,每个字符位置字符串实例。 字符串相等运算符比较两个字符串值,而不是字符串引用。当两个分离的字符串实例包含字符的确切相同的序列,所述字符串的值是相等的,但是参考文献是不同的。如第7.9.6所述,引用类型相等运算符可以用来比较两个字符串值的字符串,而不是引用

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.

这篇关于什么是不同的字符串比较方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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