c# - 检查字符串不为空、不为空且不为 "0000"的最简单方法是什么?

标签 c#

我正在使用以下内容:

!string.IsNullOrEmpty(Model.ProductID)

但现在我还需要检查字符串是否不等于“0000”。进行此检查最简单的方法是什么?

最佳答案

!string.IsNullOrEmpty(Model.ProductID) && Model.ProductID != "0000"

或者写一个扩展方法:

public static class StringExtensions
{
    public static bool IsNullEmptyOrZeros(this string value)
    {
        return !string.IsNullOrEmpty(value) && value != "0000";
    }
}

然后:

if (!Model.ProductID.IsNullEmptyOrZeros())
{
    ...
}

关于c# - 检查字符串不为空、不为空且不为 "0000"的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8552301/

相关文章:

c# - java 中 C# System.Windows.Forms.SendKeys.Send 方法的替代方法

c# - ASP.NET MVC : read route param inside controller

c# - 以编程方式在资源中使用事件 setter 设置样式

c# - 如何检查列表中是否存在整数?

c# - 使用 AForge.NET 和 c# 进行面部检测(在面部绘制矩形)

c# - 是否有 VC++ 的依赖管理器?

c# - 使用文本框调用 C#

c# - 从另一个 View 访问 View 的功能

c# - 使用 if 接口(interface)

c# - 参数化 Where IN 子句不适用于 CosmosClient QueryDefinition 对象