C# 中无大小写比较字符串
假设我有两个字符串:a 和 b。 为了比较忽略大小写时 a 和 be 是否具有相同的值,我总是使用:
// (Assume a and b have been verified not to be null)
if (a.ToLower() == b.ToLower())
但是,使用 Reflector,我在 .NET Framework 中见过几次:
// (arg three is ignoreCase)
if (string.Compare(a, b, true) == 0)
我测试了哪个更快,并且 ToLower()
每次都用我使用的字符串击败 Compare()
。
是否有理由使用 Compare()
而不是 ToLower()
? 有关不同 CultureInfo
的信息吗? 我正在挠头。
Let's say I have two strings: a and b. To compare whether a and be have the same values when case is ignored, I've always used:
// (Assume a and b have been verified not to be null)
if (a.ToLower() == b.ToLower())
However, using Reflector, I've seen this a few times in the .NET Framework:
// (arg three is ignoreCase)
if (string.Compare(a, b, true) == 0)
I tested which is faster, and the ToLower()
beat Compare()
every time with the strings I used.
Is there a reason why to Compare()
instead of ToLower()
? Something about different CultureInfo
? I'm scratching my head.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该关心的主要问题不是性能,而是正确性,从这个方面来看,您可能想要用于不区分大小写比较的方法是:
或
(第一个很有用如果您知道字符串可能为空;如果您已经知道至少一个字符串为非空,则后者编写起来更简单,我从未测试过性能,但假设它会类似。)
Ordinal 或
OrdinalIgnoreCase
是一个安全的选择,除非您知道要使用其他比较方法; 要获取做出决定所需的信息阅读 MSDN 上的这篇文章。The main thing you should be concerned about isn't performance, it's correctness, and from that aspect the method you probably want to be using for a case insensitive comparison is either:
or
(The first one is useful if you know the strings may be null; the latter is simpler to write if you already know that at least one string is non-null. I've never tested the performance but assume it will be similar.)
Ordinal
orOrdinalIgnoreCase
are a safe bet unless you know you want to use another comparison method; to get the information needed to make the decision read this article on MSDN.MSDN 文章 的“备注”部分应该对此进行解释。 本质上,原因是不同文化环境之间的兼容性。
The Remarks section of the MSDN article should explain things. Essentially, the reason is for compatibility across different cultures settings.
比较字符串时,您应该始终使用显式 StringComparison 成员。 字符串函数在比较字符串的方式上有些不一致。 保证所使用的比较的唯一方法是 a) 记住所有这些(这包括您和团队中的每个人)或 b) 对每个函数使用显式比较。
最好是明确的,而不是依赖于群体知识的完美。 你的队友会为此感谢你。
示例:
使用 ToLower 进行比较有两个问题我能立即想到
When comparing strings you should always use an explicit StringComparison member. The String functions are somewhat inconsistent in how they choose to compare strings. The only way to guarantee the comparision used is to a) memorize all of them (this includes both you and everyone on your team) or b) use an explicit comparison for every function.
It's much better to be explicit and not rely on group knowledge being perfect. Your teammates will thank you for this.
Example:
Using ToLower for comparison has 2 problems I can think of off the top of my head
另一篇 MSDN 文章提供了一些注意事项以及在各种情况下使用哪种比较方法的建议:在 Microsoft .NET 2.0 中使用字符串的新建议
Another MSDN article that provides some DOs and DON'Ts and recommendations for what comparison method to use in various cases: New Recommendations for Using Strings in Microsoft .NET 2.0
ToLower() 不是比较函数,它将字符串变为小写。 当 == 运算符用于 C# 中的 String 对象时,编译器会对其进行优化。 从本质上讲,两者都依赖于 System.String.Equals,如 Reflector 中所示。
ToLower() is not a comparison function, it puts the string to lower case. When the == operator is used on String objects in C# it is optimized by the compiler. At the core, Both depend on System.String.Equals as seen in Reflector.
您能否发布您的测试,显示调用 ToLower() 比不区分大小写的比较更快? 我的测试表明事实恰恰相反! 无论如何,其他发帖者关于正确性的观点是站得住脚的。
Could your post your test that shows calling ToLower() is faster than a case-insensitive comparison? My tests show the opposite to be true! Regardless, other posters' points about correctness stand.