Caselessly比较字符串在C# [英] Caselessly comparing strings in C#

查看:249
本文介绍了Caselessly比较字符串在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有两个字符串:a和b。比较是否并具有相同的值时忽略大小写,我一直使用:

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())

然而,使用反射,我已经在.NET Framework中看到了这几次:

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)

我测试速度更快,而 TOLOWER()比较()的字符串每次我用了。

I tested which is faster, and the ToLower() beat Compare() every time with the strings I used.

还有一个原因为什么比较()而不是 TOLOWER()?一些关于不同的的CultureInfo ?我抓我的头。

Is there a reason why to Compare() instead of ToLower()? Something about different CultureInfo? I'm scratching my head.

推荐答案

MSDN文章应该解释的东西。从本质上讲,其原因是在不同的文化设置的兼容性。

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

这篇关于Caselessly比较字符串在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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