【问题标题】:string.IsNullOrEmpty using Trim()string.IsNullOrEmpty 使用 Trim()
【发布时间】:2012-03-14 17:50:35
【问题描述】:

我要删除第一行:

                 !string.IsNullOrEmpty(cell.Text) 

这会导致任何问题吗?

我在一些代码中遇到了这个问题:

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

我认为第一个 string.IsNullOrEmpty 会在带有空格的字符串上返回 false
并且带有 Trim() 的行会处理这个问题,所以第一个 IsNullOrEmpty 没用

但在我删除没有修剪的线之前,我想我会按组运行它。

【问题讨论】:

  • 你的意思是 string.IsNullOrWhitespace
  • 是不是太难测试或者找官方文档了? (msdn.microsoft.com/en-us/library/…)。简短回答:不,一串空格不为空或为空。
  • 你们没有抓住重点。是的,您可以使用IsNullOrEmpty,但 OP 对为什么需要第一次调用来避免NullReferenceException 感到困惑。理解这一点比切换到不同的方法调用重要得多,因为他被告知这样做。
  • @madth3 我认为他实际上是在问他是否可以省略第一次检查:!string.IsNullOrEmpty(cell.Text),而不是 string.IsNullOrEmpty() 是否检查空格,但不得不阅读这个问题几次。跨度>
  • @ntziolis 你说得对,我误解了要删除的行。我仍然认为尝试一下并查看官方文档并不难。

标签: c# string trim isnullorempty


【解决方案1】:

如果 cell.Text 为 null,那么如果不进行第一次检查,就会出现异常。

【讨论】:

  • 如果单元格是文本框,则该属性永远不会为空。
  • 如果您还有 .NET cell.Text != null && cell.Text.Trim() != "" 来实现。
  • 注意:单元格是使用 WebDriver 获取的 html 表格单元格内容(但这不是问题的重点)
【解决方案2】:

在 .NET 4.0 中:

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

在旧版本中,您应该保留这两个测试,因为如果您删除第一个并且 cell.Text 为空,那么当您尝试在空实例上调用 .Trim 时,您将在第二个获得 NRE。

或者你也可以这样做:

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

或者更好的是,您可以为字符串类型编写一个extension method,这样您就可以简单地:

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

可能看起来像这样:

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

【讨论】:

  • 我不认为在这种情况下扩展方法更好。根据经验,“不要接受 null 作为有效的扩展方法参数”。其他程序员可能会将其解释为“文本必须具有值,因为可以调用方法”。
【解决方案3】:

第一个 IsNullOrEmpty 在使用 Trim() 抛出 NullReferenceException 之前捕获空值。

不过,还有更好的办法:

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

【讨论】:

    【解决方案4】:

    你可以像这样使用扩展方法:

    /// <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;
    }
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          您不能只删除第一个 IsNullOrEmpty,因为 cell.Text 可能为 null,因此在其上调用 Trim 会引发异常。如果您使用的是 .Net 4.0,请使用 IsNullOrWhiteSpace,或者保留这两项检查。

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

          【讨论】:

          • 是的,但这并不能回答问题。显然,OP 对如何评估 if 语句表达式感到困惑。
          【解决方案8】:

          如果 cell.Text 为 null,则表达式 string.IsNullOrEmpty(cell.Text.Trim()) 将抛出异常,因为它试图在单元格上运行方法 Trim()。

          如果条件是: cell.Text!=null && cell.Text.Trim()!=""

          【讨论】:

            【解决方案9】:

            我们可以使用空条件:

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

            注意“?”在阅读下一个属性和修剪之前。

            【讨论】:

              猜你喜欢
              • 2011-01-25
              • 2010-09-30
              • 1970-01-01
              • 2020-11-03
              • 1970-01-01
              • 1970-01-01
              • 2017-02-06
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多