简体   繁体   English

string.IsNullOrEmpty 使用 Trim()

[英]string.IsNullOrEmpty using Trim()

I want to remove the first line:我想删除第一行:

                 !string.IsNullOrEmpty(cell.Text) 

will this cause any issue?这会导致任何问题吗?

I ran across this in some code:我在一些代码中遇到了这个:

                if ((id % 2 == 0)
                    && !string.IsNullOrEmpty(cell.Text)
                    && !string.IsNullOrEmpty(cell.Text.Trim())
                    )

I think the first string.IsNullOrEmpty would return false on a string with spaces我认为第一个 string.IsNullOrEmpty 会在带空格的字符串上返回 false
and the line with Trim() takes care of that, so the first IsNullOrEmpty is useless和 Trim() 的行负责处理,所以第一个 IsNullOrEmpty 没用

But before I remove the line without the trim I thought I'd run it by the group.但在我删除没有修剪的线之前,我想我应该由小组来运行它。

如果cell.Text为null,则没有第一次检查就会出现异常。

In .NET 4.0: 在.NET 4.0中:

if (id % 2 == 0 && !string.IsNullOrWhiteSpace(cell.Text))
{
    ...
}

In older versions you should keep the two tests because if you remove the first and cell.Text is null, you will get a NRE on the second when you try to invoke .Trim on a null instance. 在旧版本中,您应该保留两个测试,因为如果删除第一个并且cell.Text为null,当您尝试在空实例上调用.Trim时,将在第二个上获得NRE。

Or you could also do this: 或者你也可以这样做:

if (id % 2 == 0 && string.IsNullOrWhiteSpace((cell.Text ?? string.Empty).Trim()))
{
    ...
}

or even better, you could write an extension method for the string type that will do this so that you could simply: 甚至更好,您可以为字符串类型编写一个扩展方法来执行此操作,以便您可以简单地:

if (id % 2 == 0 && !cell.Text.IsNullOrWhiteSpace())
{
    ...
}

which might look like this: 这看起来像这样:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        return string.IsNullOrEmpty((value ?? string.Empty).Trim());
    }
}

The first IsNullOrEmpty catches null values before they throw a NullReferenceException with Trim(). 第一个IsNullOrEmpty在使用Trim()抛出NullReferenceException之前捕获空值。

However, there is a better way: 但是,有一种更好的方法:

if ((id % 2 == 0) && !string.IsNullOrWhiteSpace(cell.Text))

You can use extension method like this: 你可以像这样使用扩展方法:

/// <summary>
/// Indicates whether the specified string is null or empty.
/// This methods internally uses string.IsNullOrEmpty by trimming the string first which string.IsNullOrEmpty doesn't.
/// .NET's default string.IsNullOrEmpty method return false if a string is just having one blank space.
/// For such cases this custom IsNullOrEmptyWithTrim method is useful.
/// </summary>
/// <returns><c>true</c> if the string is null or empty or just having blank spaces;<c>false</c> otherwise.</returns> 
public static bool IsNullOrEmptyWithTrim(this string value)
{
    bool isEmpty = string.IsNullOrEmpty(value);
    if (isEmpty)
    {
        return true;
    }
    return value.Trim().Length == 0;
}

We can use null conditional:我们可以使用 null 条件:

!string.IsNullOrEmpty(cell?.Text?.Trim())

Note "?"笔记 ”?” before reading the next property and trim.在阅读下一个属性和修剪之前。

我相信测试是为了确保cell.text首先不是null ...如果是这样,试图绕过它并得到只是cell.text.trim()会因为你无法对空字符串进行修剪而窒息。

为什么不使用!string.IsNullOrWhitespace(call.Text)并删除前两个检查?

You cannot remove just the first IsNullOrEmpty as the cell.Text could be null and thus calling Trim on it would throw and exception. 你不能只删除第一个IsNullOrEmpty作为cell.Text可能为null,因此在它上面调用Trim将抛出异常。 Use IsNullOrWhiteSpace if you are using .Net 4.0 or leave both checks. 如果您使用.Net 4.0或使用两个检查,请使用IsNullOrWhiteSpace

if ((id % 2 == 0) && !string.IsNullOrWhiteSpace(cell.Text))

If cell.Text is null, the expression string.IsNullOrEmpty(cell.Text.Trim()) will throw an exception since it is trying to run method Trim() on cell. 如果cell.Text为null,则表达式string.IsNullOrEmpty(cell.Text.Trim())将抛出异常,因为它试图在单元格上运行方法Trim()。

Much more readble if condition would be: cell.Text!=null && cell.Text.Trim()!="" 如果条件如下,则更多可读:cell.Text!= null && cell.Text.Trim()!=“”

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

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