C# 具有空处理的字符串相等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15188079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:24:04  来源:igfitidea点击:

String equality with null handling

c#.netstringnullequality

提问by Brady Moritz

I will often use this code to compare a string:

我会经常使用这段代码来比较一个字符串:

if(!string.IsNullOrEmpty(str1) && str1.Equals(str2)){
    //they are equal, do my thing
}

This handles the null case first etc.

这首先处理空情况等。

Is there a cleaner way to do string comparison, perhaps with a single method call that will handle possible null values? I simply want to know that the strings are not equal if the testing value is null.

是否有更简洁的方法来进行字符串比较,也许使用单个方法调用来处理可能的空值?我只是想知道如果测试值为空,则字符串不相等。

(I'm having dejavu that I may have asked this before, I apologize if so)

(我之前可能已经问过这个问题,如果是这样,我很抱歉)



Update: In my case, the str2 is a known good string to compare to, so I don't need to check it for null. str1 is the "unknown" string which may be null, so I want to say "str1 does not equal str2" in the cases where str1 isnull...

更新:在我的例子中,str2 是一个已知的好字符串来比较,所以我不需要检查它是否为空。str1 是可能为空的“未知”字符串,所以我想在 str1空的情况下说“str1 不等于 str2” ...

回答by SLaks

Unlike Java, C# strings override the ==operator:

与 Java 不同,C# 字符串会覆盖==运算符:

if (str1 == str2)

If you want a case-insensitive comparison:

如果您想要不区分大小写的比较:

if (String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase)

回答by dasblinkenlight

If you do notwant to treat two nullstrings as equal to each other, your code is optimal.

如果你希望处理两个null字符串作为彼此相等,你的代码是最优的。

If, on the other hand, you want to treat nullvalues as equal to each other, you can use

另一方面,如果您想将null值视为彼此相等,则可以使用

object.Equals(str1, str2)

for a more "symmetric" approach that also handles nullvalues.

对于也处理null值的更“对称”的方法。

回答by dasblinkenlight

You can use this code

您可以使用此代码

object.Equals(str1, str2)

回答by Justin Pihony

There is no built in way to do this, but you could create an extension method to encapsulate this.

没有内置的方法可以做到这一点,但您可以创建一个扩展方法来封装它。

public static StringExtensions
{
    public static Boolean IsNotNullAndEquals(this string str1, string str2)
    {
        return !string.IsNullOrEmpty(str1) && str1.Equals(str2)
    }
}

then use it like this:

然后像这样使用它:

str1.IsNotNullAndEquals(str2);

Naming is going to be your hardest thing here IMO...since you need to convey that you are only null checking str1. When used as an extension method, it reads fairly well, but if used as a regular static, then it doesn't convey that as well.

在这里,命名将是 IMO 中最困难的事情……因为您需要传达您只是空检查str1。当用作扩展方法时,它读起来相当好,但如果用作常规静态,那么它也不会传达这一点。

回答by dotsven

I know this is some years old and I think the solution from dasblinkenlight is functionally perfect for what you asked for. However I do prefer this code for readability reasons:

我知道这已经有些年头了,我认为 dasblinkenlight 的解决方案在功能上非常适合您的要求。但是,出于可读性原因,我更喜欢此代码:

String.Equals(str1, str2)

回答by Emad Jazer

This will do it:

这将做到:

string.IsNullOrWhiteSpace(str1) ? string.IsNullOrWhiteSpace(str2) : str1.Equals(str2, StringComparison.OrdinalIgnoreCase);

回答by Brady Moritz

In more recent C# versions, we also have the null coalescing option now. This isn't an exact solution but can be a cleaner one for some purposes. It takes some liberties with "upgrading" str1 to an empty string for comparison purposes, in the instances where it actually is a null.

在最近的 C# 版本中,我们现在也有空合并选项。这不是一个精确的解决方案,但出于某些目的可以是一个更清洁的解决方案。在实际为空的情况下,出于比较目的,将 str1 “升级”为空字符串需要一些自由。

if((str1?"").Equals(str2))
{
    //they are equal, do things here
}