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

查看:127
本文介绍了字符串比较 - 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.)

例如,你想让代码在显示mail 当它在土耳其运行?如果你使用 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.

请参阅 MSDN中使用字符串的建议

除了别的,我会说,后者更有效地表达你的意图。你实际上对字符串的小写字母值并不感兴趣 - 你在不区分大小写的方式中感兴趣 ...这正是第二个形式表达。

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天全站免登陆