大写与小写 [英] Upper vs Lower Case

查看:157
本文介绍了大写与小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进行不区分大小写的比较时,将字符串转换为大写还是小写更有效?甚至有关系吗?

When doing case-insensitive comparisons, is it more efficient to convert the string to upper case or lower case? Does it even matter?

在本SO帖子中建议 建议使用ToUpper来提高C#的效率,因为微软以这种方式对其进行了优化."但我也阅读了此论点,认为将ToLower与ToUpper转换取决于您的字符串包含更多,并且通常字符串包含更多小写字符,这使ToLower更加有效.

It is suggested in this SO post that C# is more efficient with ToUpper because "Microsoft optimized it that way." But I've also read this argument that converting ToLower vs. ToUpper depends on what your strings contain more of, and that typically strings contain more lower case characters which makes ToLower more efficient.

我特别想知道:

  • 是否有一种方法可以优化ToUpper或ToLower,使其中一个比另一个更快?
  • 在大写或小写字符串之间进行不区分大小写的比较是否更快?为什么?
  • 是否存在任何编程环境(例如C,C#,Python等),其中一种情况明显优于另一种情况,为什么?

推荐答案

转换为大写或小写以进行不区分大小写的比较是不正确的,因为某些文化(尤其是土耳其)具有有趣的"特征.取而代之的是,使用 StringComparer ,提供适当的选项.

Converting to either upper case or lower case in order to do case-insensitive comparisons is incorrect due to "interesting" features of some cultures, particularly Turkey. Instead, use a StringComparer with the appropriate options.

MSDN在字符串处理方面有一些绝佳指南.您可能还需要检查您的代码是否通过了 the土耳其测试.

MSDN has some great guidelines on string handling. You might also want to check that your code passes the Turkey test.

注意Neil对 ordinal 不区分大小写的比较的评论.这整个境界都是阴暗的:(

Note Neil's comment around ordinal case-insensitive comparisons. This whole realm is pretty murky :(

这篇关于大写与小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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