在C#中比较两个字符串的最佳方法是什么? [英] what is the best way to compare two strings in c#?
本文介绍了在C#中比较两个字符串的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问有人可以告诉我在C#中比较字符串的最佳方法是什么吗?
谢谢大家.
please can anyone advice me what is the best way to compare strings in c#?
Thanks all.
推荐答案
建议使用Equals
方法(静态string.Equals
或Equals
extension方法).请参见 http://msdn.microsoft.com/en-us/library/cc165449.aspx [ ^ ]
It is recommended that theEquals
method be used, either the staticstring.Equals
or theEquals
extension method. See http://msdn.microsoft.com/en-us/library/cc165449.aspx[^]
我想如果您要比较equality
,然后使用Equals instance
方法 http://msdn.microsoft.com/en-us/library/c64xh8f9 [ ^ ]可能更好,因为它还提供了说明Culture
和Ignorecase
的选项,如此处http://msdn.microsoft.com/en-us/library/system.stringcomparison [ http://msdn.microsoft.com/en-us/library/cc190529 [ ^ ]更好
作为字符串的CompareTo
实例方法, http://msdn.microsoft.com/en-us/library/35f0x18w [^ ]不提供那些选项.
I think if you want to compare forequality
then theEquals instance
method http://msdn.microsoft.com/en-us/library/c64xh8f9[^] of string may be better as it also gives options to account forCulture
andIgnorecase
as given here http://msdn.microsoft.com/en-us/library/system.stringcomparison[^]
If you want to compare to decide theorder
in which thetwo strings
are to be placed and want to have options ofCulture
andIgnoreCase
etc. then theCompare
static
method ofstring
, http://msdn.microsoft.com/en-us/library/cc190529[^] is better
as theCompareTo
instance method of string, http://msdn.microsoft.com/en-us/library/35f0x18w[^] does not provide those options.
最佳"(具有讽刺意味的是使用了该术语)方法是"==;它进行语义比较.它不是多余的,而且更具可读性.而System.Object.ReferenceEquals
为您提供参考比较.顺便说一句,您可以为类型定义自己的相等性(通常需要这样做),并且这样就必须使用引用相等性,因为否则会出现堆栈溢出(如果定义Equals
或"=="并在定义中使用它,通常用于与null进行比较;您是否需要解释原因?).可以使用System.String.CompareTo
来获取">" or "<"
的语义.
请参阅:
http://msdn.microsoft.com/en-us/library/system.string.aspx [ ^ ].
选择适合您目的的方法.
我几乎忘记了:语义字符串比较可能区分大小写,并且取决于文化.我的意思是,在一种文化中,两个技术上不同的字符串可能被认为是相等的,或者比另一个字符串少一个,但是在另一种文化中,结果可能会有所不同.请参阅所有System.String.Compare
方法,请参见上面的参考.
—SA
The "best" (the term is used ironically) method is "=="; it makes semantic comparison. It is not redundant and is more readable. AndSystem.Object.ReferenceEquals
gives you referential comparison. By the way, you can define your own equality (this is often needed) for your type, and in this way using referential equality is the must, because otherwise you would have stack overflow (if you defineEquals
or "==" and use it in the definition, usually for comparison with null; do you need to explain why?). The semantic of">" or "<"
can be got usingSystem.String.CompareTo
.
Please see:
http://msdn.microsoft.com/en-us/library/system.string.aspx[^].
Choose those methods which fit your purpose.
I almost forgot: semantic string comparison may be case-sensitive or not and depend on culture. I mean, in one culture two technically different strings may be considered equal, or one less then another, but the result could be different in another culture. See all theSystem.String.Compare
method, see the reference above.
—SA
这篇关于在C#中比较两个字符串的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文