简体   繁体   English

检查字符串是否为空

[英]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). 但是,即使字符串为"" ,程序仍在执行IF(我不想发生这种情况)。

Use String.IsNullOrWhiteSpace as mentioned below 如下所述使用String.IsNullOrWhiteSpace

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.IsNullOrEmpty ,它专为检查值是空引用还是对长度为0的字符串的引用设计,尽管它并非旨在检查全空格字符串。 String.IsNullOrWhiteSpace will handle that, but it's only available from .NET 4 upwards. String.IsNullOrWhiteSpace将处理此问题,但仅在.NET 4以上版本中可用。 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. 在您的脑海中区分一个字符串(长度为0的字符串)和一个全空格字符串非常重要-您的问题询问一个空字符串,但是您要在检查空性之前在代码中修剪字符串,这表明您实际上只是想知道它是否为全空格。

The result of Trim will never be a null reference, although it might be a reference to an empty string. 尽管Trim的结果可能是对空字符串的引用,但它的结果永远不会为空引用。 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. 因为Trim()永远不会返回空字符串的引用空引用。

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: 另外,如果您对调整后的值确实很感兴趣,则可能应该在代码中始终使用它-此时,您可能需要将对null的检查和对空字符串的检查分开:

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. 如果您使用的是.NET 4,则后者更干净-性能差异不太可能很大。

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 如果您打算检查Whitespace ,则需要使用String.IsNullOrWhiteSpace()方法

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. 因为它检查字符串为null,为空或仅包含空格。

while String.IsNullOrEmpty will not check white-spaces. String.IsNullOrEmpty将不检查空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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