Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to check that a string is not null, not empty and not = "0000"?

Tags:

c#

I was using the following:

!string.IsNullOrEmpty(Model.ProductID)

But now I need to also check that the string is not equal to "0000". What's the most easy way to do this check?

like image 882
Samantha J T Star Avatar asked Dec 28 '22 09:12

Samantha J T Star


2 Answers

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

or write an extension method:

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

and then:

if (!Model.ProductID.IsNullEmptyOrZeros())
{
    ...
}
like image 185
Darin Dimitrov Avatar answered Feb 08 '23 11:02

Darin Dimitrov


if(!string.isNullOrEmpty(Model.ProductID) && Model.ProductID != "0000")
like image 25
Tudor Avatar answered Feb 08 '23 09:02

Tudor