字符串比较 - strA.ToLower()== strB.ToLower()或strA.Equals(STRB,StringComparisonType)? [英] String comparison - strA.ToLower()==strB.ToLower() or strA.Equals(strB,StringComparisonType)?

查看:125
本文介绍了字符串比较 - strA.ToLower()== strB.ToLower()或strA.Equals(STRB,StringComparisonType)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按标题,什么做法字符串比较你用,为什么?

As per title, what practise for string comparisons do you use and why?

推荐答案

您还没有指定一个平台,但我猜.NET。我强烈建议您使用一种形式 - 因为情况比较并不像你想象的那么简单。 (这也避免了创造额外的字符串,但是这是一个不同的问题。)

You haven't specified a platform, but I'm guessing .NET. I would strongly suggest you use the latter form - because case comparisons aren't as simple as you might expect. (It also avoids creating extra strings, but that's a different matter.)

例如,你想在$ P $当它在土耳其的运行具有邮件和邮件psented你code做什么?如果你使用 ToLower将将返回false,并且同样,如果你使用 CurrentCultureIgnoreCase - 但如果你使用 InvariantCultureIgnoreCase 它将返回true。你需要考虑数据的来源,你想用它达到什么目的。

For example, what do you want your code to do when presented with "mail" and "MAIL" when it's running in Turkey? If you use ToLower it will return false, and likewise if you use CurrentCultureIgnoreCase - but if you use InvariantCultureIgnoreCase it will return true. You need to think about the source of the data and what you're trying achieve with it.

请参阅href=\"http://msdn.microsoft.com/en-us/library/ms973919.aspx\">使用字符串建议在MSDN 了解更多信息和指导

See the recommendations for using strings in MSDN for more information and guidance.

除了别的,我会说,后者前$ P $更有效地psses你的意图。你不是在字符串的小写值真正感兴趣的 - 你有兴趣的平等的的区分大小写的方式......这正是第二个的形式EX presses。

Aside from anything else, I'd say that the latter expresses your intent more effectively. You're not actually interested in the lower case values of the strings - you're interested in equality in a case-insensitive way... which is exactly what the second form expresses.

这篇关于字符串比较 - strA.ToLower()== strB.ToLower()或strA.Equals(STRB,StringComparisonType)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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