字符串比较等效项

发布于 2024-08-13 18:14:07 字数 289 浏览 3 评论 0原文

我相信这两行是等效的,但在遇到一个奇怪的问题后,我不再相信情况是这样。

String mimeType = context.Request.ContentType;
(String.Compare("text/xml", mimeType, true) == 0))

与 :

context.Request.ContentType.ToLower().Equals("text/xml")

它们在 CLR 中的实现有什么不同吗?

I believe these 2 lines are equivalent but after running into a strange issue I no longer believe this to be the case.

String mimeType = context.Request.ContentType;
(String.Compare("text/xml", mimeType, true) == 0))

is the same as :

context.Request.ContentType.ToLower().Equals("text/xml")

Are their implementations in the CLR any different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

夕色琉璃 2024-08-20 18:14:07

它们并不等同,ToLower/ToUpper 可能存在一些本地化问题。比较两个字符串而不区分大小写的方法(考虑到其中一个字符串可能为 null,这就是我不喜欢 str1.Equals 方法的原因)是静态 String.Equals 方法:

bool areEqual = String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);

They are not equivalent, and ToLower/ToUpper may have some localization issues. The way to compare two strings without case-sensitivity (considering one of the strings may be null, which is why I don't like the str1.Equals method) is the static String.Equals method:

bool areEqual = String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);
苯莒 2024-08-20 18:14:07

它们并不完全等同;请参阅此处

以下是进行不区分大小写比较的正确方法:

bool areSame = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);

这种方法也会更有效,因为它不会为小写副本分配单独的字符串。

They are not completely equivalent; see here.

Here is the correct way to do a case insensitive comparison:

bool areSame = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);

This way will also be more efficient becasue it doesn't allocate a separate string for the lowercase copy.

绅士风度i 2024-08-20 18:14:07

除了其他答案(@SLaks,@Serhio)之外,我还有义务指出 .ToLower() 生成另一个字符串。据我所知,比较没有。如果应用程序中的过多字符串生成在频繁调用的代码中,则可能会在内存使用和性能方面造成影响。

In addition to the other answers (@SLaks, @Serhio), I also feel obligated to point out that .ToLower() generates another string. Compare does not as far as I know. Excessive string generation in an app can come back to bite you in terms of memory usage and performance if it is in frequently called code.

断桥再见 2024-08-20 18:14:07

.NET 中 Compare(string, string, boolean) 的实现:

public static int Compare(string strA, string strB, bool ignoreCase)
{
    if (ignoreCase)
    {
        return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreCase);
    }
    return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.None);
}

和 Equals

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public bool Equals(string value)
{
    if ((value == null) && (this != null))
    {
        return false;
    }
    return EqualsHelper(this, value);
}

So 是同一件事。

the implementation of Compare(string, string, boolean) in .NET:

public static int Compare(string strA, string strB, bool ignoreCase)
{
    if (ignoreCase)
    {
        return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreCase);
    }
    return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.None);
}

and Equals

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public bool Equals(string value)
{
    if ((value == null) && (this != null))
    {
        return false;
    }
    return EqualsHelper(this, value);
}

So, is NOT the same thing.

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