C# 中无大小写比较字符串

发布于 2024-07-13 13:13:53 字数 512 浏览 5 评论 0原文

假设我有两个字符串: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

执手闯天涯 2024-07-20 13:13:53

您应该关心的主要问题不是性能,而是正确性,从这个方面来看,您可能想要用于不区分大小写比较的方法是:

string.Compare(a, b, StringComparison.OrdinalIgnoreCase) == 0;

a.Equals(b, StringComparison.OrdinalIgnoreCase)

(第一个很有用如果您知道字符串可能为空;如果您已经知道至少一个字符串为非空,则后者编写起来更简单,我从未测试过性能,但假设它会类似。)

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:

string.Compare(a, b, StringComparison.OrdinalIgnoreCase) == 0;

or

a.Equals(b, StringComparison.OrdinalIgnoreCase)

(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 or OrdinalIgnoreCase 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.

江挽川 2024-07-20 13:13:53

MSDN 文章 的“备注”部分应该对此进行解释。 本质上,原因是不同文化环境之间的兼容性。

The Remarks section of the MSDN article should explain things. Essentially, the reason is for compatibility across different cultures settings.

娇纵 2024-07-20 13:13:53

比较字符串时,您应该始终使用显式 StringComparison 成员。 字符串函数在比较字符串的方式上有些不一致。 保证所使用的比较的唯一方法是 a) 记住所有这些(这包括您和团队中的每个人)或 b) 对每个函数使用显式比较。

最好是明确的,而不是依赖于群体知识的完美。 你的队友会为此感谢你。

示例:

if ( StringComparison.OrdinalIgnoreCase.Equals(a,b) )

使用 ToLower 进行比较有两个问题我能立即想到

  1. 它分配内存。 除非绝对必要,否则比较函数不应分配内存。
  2. 可以通过多种方式降低琴弦。 最值得注意的是序数或文化敏感性较低。 .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:

if ( StringComparison.OrdinalIgnoreCase.Equals(a,b) )

Using ToLower for comparison has 2 problems I can think of off the top of my head

  1. It allocates memory. Comparison functions should not allocate memory unless they absolutely have to.
  2. Strings can be lowered in several ways. Most notable Ordinal or Culture Sensitive lower. Which way does .ToLower() work? Personally, I don't know. It's much better to pass an explicit culture than rely on the default.
流绪微梦 2024-07-20 13:13:53

另一篇 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

仲春光 2024-07-20 13:13:53

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.

云之铃。 2024-07-20 13:13:53

您能否发布您的测试,显示调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文