在 C# 中编写 null 或empty 的更简单方法?

发布于 2024-12-07 12:25:13 字数 140 浏览 6 评论 0原文

我确信我在这里错过了一些东西。对于某个项目,我需要检查字符串是否为空或空。

有没有更简单的方法来写这个?

if (myString == null || myString == "")
{
   ...

I'm sure I've missed something here. With a certain project I need to check if a string is null or empty.

Is there an easier way of writing this?

if (myString == null || myString == "")
{
   ...

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

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

发布评论

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

评论(7

淡水深流 2024-12-14 12:25:13

是的,有 String.IsNullOrEmpty< /a> 辅助方法已经实现了这一点:

if (String.IsNullOrEmpty(myString)) {
    ...
}

Yes, there's the String.IsNullOrEmpty helper method for exactly this already:

if (String.IsNullOrEmpty(myString)) {
    ...
}
我家小可爱 2024-12-14 12:25:13
if (string.IsNullOrEmpty(myString)) {
  ...
}

或者您可以利用扩展方法中的一个怪癖,它们允许 this 为 null:

static class Extensions {
    public static bool IsEmpty(this string s) {
        return string.IsNullOrEmpty(s);
    }
}

然后让您编写:

if (myString.IsEmpty()) {
  ...
}

尽管您可能应该选择“空”之外的其他名称。

if (string.IsNullOrEmpty(myString)) {
  ...
}

Or you could take advantage of a quirk in extension methods, they allow this to be null:

static class Extensions {
    public static bool IsEmpty(this string s) {
        return string.IsNullOrEmpty(s);
    }
}

which then lets you write:

if (myString.IsEmpty()) {
  ...
}

Although you probably should pick another name than 'empty'.

最美的太阳 2024-12-14 12:25:13

如果您使用的是 .NET 4,则可以使用

if(string.IsNullOrWhiteSpace(myString)){

}

else:

if(string.IsNullOrEmpty(myString)){

}

If you are on .NET 4, you can use

if(string.IsNullOrWhiteSpace(myString)){

}

else:

if(string.IsNullOrEmpty(myString)){

}
无声静候 2024-12-14 12:25:13

为了避免空检查,您可以使用 ??操作员。

var result = value ?? "";

我经常使用它作为警卫,以避免在方法中发送我不需要的数据。

JoinStrings(value1 ?? "", value2 ?? "")

它还可用于避免不必要的格式化。

string ToString()
{
    return "[" + (value1 ?? 0.0) + ", " + (value2 ?? 0.0) + "]";
}

这也可以用在 if 语句中,虽然不太好,但有时很方便。

if (value ?? "" != "") // Not the best example.
{
}

To avoid null checks you can use ?? operator.

var result = value ?? "";

I often use it as guards to avoid sending in data that I don't want in methods.

JoinStrings(value1 ?? "", value2 ?? "")

It can also be used to avoid unwanted formatting.

string ToString()
{
    return "[" + (value1 ?? 0.0) + ", " + (value2 ?? 0.0) + "]";
}

This can also be used in if statements, it's not so nice but can be handy sometimes.

if (value ?? "" != "") // Not the best example.
{
}
长亭外,古道边 2024-12-14 12:25:13

在 c# 9 中,通过使用模式匹配,您可以执行以下操作

myString is not {Length: > 0}; // Equivalent to string.IsNullOrEmpty(myString)

In c# 9 by using pattern matching you could do the following

myString is not {Length: > 0}; // Equivalent to string.IsNullOrEmpty(myString)
何其悲哀 2024-12-14 12:25:13

C# 9 的模式匹配允许您编写:

myString is null or ""

C# 9's pattern matching allows you to write:

myString is null or ""
抹茶夏天i‖ 2024-12-14 12:25:13

// 如果字符串未定义为 null,则 IsNullOrEmpty 效果很好,但如果字符串定义为 null,则修剪将抛出异常。

if(string.IsNullOrEmpty(myString.Trim()){
 ...
}

//您可以使用 IsNullOrWhiteSpace ,它对于字符串中的多个空格效果很好。即它对于多个空格也返回 true

 if(string.IsNullOrWhiteSpace (myString.Trim()){
     ...
    }

// if the string is not defined to null then IsNullOrEmpty it works great but if string is defined null then trim will throw exception.

if(string.IsNullOrEmpty(myString.Trim()){
 ...
}

//you can use IsNullOrWhiteSpace which work well for multiple white space in string .i.e it return true for multiple white space also

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