简体   繁体   中英

Checking if a string is null or not

Guys I have an argument to a method. I want to check if its null or empty string before further directing the program's execution path

My method is as

public void DisplayCalcQuery(string argFromQueryBuilder)
{
    if ((argFromQueryBuilder.Trim() != null) || (argFromQueryBuilder.Trim() != ""))
    {
        //notify closure of query builder
        _QueryBuilderIsOpen = false;
        UserBuiltQueries.Add(argFromQueryBuilder);
        //displayng the user built query(queries) on the stack panel meant to display it. 
        var lastItem = UserBuiltQueries[UserBuiltQueries.Count - 1];
        //removing all $signs from the obtained string
        lastItem = lastItem.Replace(@"$", "");
        addBuiltCheck(lastItem); 
    }
    else
    {
        //notify closure of query builder
        _QueryBuilderIsOpen = false;
    }
}

However, even when the string is being "" The program is executing the IF (which I do not want to happen).

Use String.IsNullOrWhiteSpace as mentioned below

public void DisplayCalcQuery(string argFromQueryBuilder)
{
    if (!string.IsNullOrWhiteSpace(argFromQueryBuilder))
    {
        //notify closure of query builder
        _QueryBuilderIsOpen = false;
        UserBuiltQueries.Add(argFromQueryBuilder);
        //displayng the user built query(queries) on the stack panel meant to display it. 
        var lastItem = UserBuiltQueries[UserBuiltQueries.Count - 1];
        //removing all $signs from the obtained string
        lastItem = lastItem.Replace(@"$", "");
        addBuiltCheck(lastItem); 
    }
    else
    {
        //notify closure of query builder
        _QueryBuilderIsOpen = false;
    }
}

Use String.IsNullOrEmpty which is precisely designed for checking whether a value is a null reference or a reference to a 0-length string - although it's not designed for checking for an all-whitespace string. String.IsNullOrWhiteSpace will handle that, but it's only available from .NET 4 upwards. It's important to distinguish (in your mind) between an empty string (one with a length of 0) and an all-whitespace string - your question asks about an empty string, but the you're trimming the string in code before checking for emptiness, which suggests you actually just want to know whether it's all-whitespace or not.

The result of Trim will never be a null reference, although it might be a reference to an empty string. Those are very different things. It's not clear why you've got an || clause either, as Trim() could never return both a reference to an empty string and a null reference.

Additionally, if you're really interested in the trimmed value, you should probably use that consistently through the code - at which point you may need to separate the checking for nullity and the checking for an empty string anyway:

if (arg == null)
{
    // Whatever
}
string trimmed = arg.Trim();
if (arg == "")
{
    // Whatever
}

Or you could take the extra hit of checking for whitespace and still trimming:

if (string.IsNullOrWhiteSpace(arg))
{
    // Whatever - make sure you return in here,
    // to avoid calling Trim on a null reference
}
string trimmed = arg.Trim();
// Whatever

The latter is cleaner if you're using .NET 4 - it's unlikely that the performance difference will be significant.

Finally, if it's valid for a caller to pass in a reference to an empty (or all-whitespace) string, but not valid for them to pass in a null reference, consider handling those situations separately:

if (arg == null)
{
    throw new ArgumentNullException("...");
}
string trimmed = arg.Trim();
if (arg == "")
{
    // Whatever
}

if your intention is to check Whitespace you need to Use the String.IsNullOrWhiteSpace() method

Try This:

if(!String.IsNullOrWhiteSpace(argFromQueryBuilder))
{
  //do here

}

Complete Code:

public void DisplayCalcQuery(string argFromQueryBuilder)
{
if (!String.IsNullOrWhiteSpace(argFromQueryBuilder))
{
    //notify closure of query builder
    _QueryBuilderIsOpen = false;
    UserBuiltQueries.Add(argFromQueryBuilder);
    //displayng the user built query(queries) on the stack panel meant to display it. 
    var lastItem = UserBuiltQueries[UserBuiltQueries.Count - 1];
    //removing all $signs from the obtained string
    lastItem = lastItem.Replace(@"$", "");
    addBuiltCheck(lastItem); 
}
else
{
    //notify closure of query builder
    _QueryBuilderIsOpen = false;
}

}

I would recommend:

String.IsNullOrWhiteSpace()

as it check string is null, empty or contains only white-spaces.

while String.IsNullOrEmpty will not check white-spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM