Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# string.IsNullOrEmpty使用Trim()命令_C#_String_Trim_Isnullorempty - Fatal编程技术网

C# string.IsNullOrEmpty使用Trim()命令

C# string.IsNullOrEmpty使用Trim()命令,c#,string,trim,isnullorempty,C#,String,Trim,Isnullorempty,我想删除第一行: !string.IsNullOrEmpty(cell.Text) 这会引起什么问题吗 我在一些代码中遇到了这个问题: if ((id % 2 == 0) && !string.IsNullOrEmpty(cell.Text) && !string.IsNullOrEmpty(cell.Text.Tr

我想删除第一行:

                 !string.IsNullOrEmpty(cell.Text) 
这会引起什么问题吗

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

                if ((id % 2 == 0)
                    && !string.IsNullOrEmpty(cell.Text)
                    && !string.IsNullOrEmpty(cell.Text.Trim())
                    )
我认为第一个string.IsNullOrEmpty将在带有空格的字符串上返回false
带有Trim()的行处理了这个问题,因此第一个IsNullOrEmpty是无用的


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

如果cell.Text为null,则在没有第一次检查的情况下会出现异常。

在.NET 4.0中:

if (id % 2 == 0 && !string.IsNullOrWhiteSpace(cell.Text))
{
    ...
}
在旧版本中,您应该保留这两个测试,因为如果删除第一个单元格,并且
单元格.Text
为null,那么当您尝试在null实例上调用
.Trim
时,第二个单元格将获得NRE

或者你也可以这样做:

if (id % 2 == 0 && string.IsNullOrWhiteSpace((cell.Text ?? string.Empty).Trim()))
{
    ...
}
或者更好的是,您可以为字符串类型编写一个

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

我相信测试首先是为了确保cell.text不为空。。。如果是这样的话,尝试绕过它并仅获取cell.text.trim()将阻塞,因为您无法对空字符串进行修剪。

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

您不能仅删除第一个IsNullOrEmpty作为单元格。Text可以为null,因此对其调用Trim将引发异常。如果您使用的是.Net 4.0,请使用,或者保留两个复选框

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

如果cell.Text为null,则表达式字符串.IsNullOrEmpty(cell.Text.Trim())将引发异常,因为它正在尝试对单元格运行方法Trim()


如果条件为:cell.Text,则可读性更高=null&&cell.Text.Trim()=“”

第一个IsNullOrEmpty在null值抛出带有Trim()的NullReferenceException之前捕获null值

但是,有一个更好的方法:

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

您可以使用如下扩展方法:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        return string.IsNullOrEmpty((value ?? string.Empty).Trim());
    }
}
/// <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;
}
//
///指示指定的字符串是null还是空。
///此方法通过首先修剪string.IsNullOrEmpty没有修剪的字符串,在内部使用string.IsNullOrEmpty。
///如果字符串只有一个空格,则.NET的默认string.IsNullOrEmpty方法返回false。
///对于这种情况,此自定义IsNullOrEmptyWithTrim方法非常有用。
/// 
///如果字符串为null或空或只有空格,则为true;否则就错了。
公共静态bool IsNullOrEmptyWithTrim(此字符串值)
{
bool isEmpty=string.IsNullOrEmpty(值);
如果(我是空的)
{
返回true;
}
返回值.Trim().Length==0;
}

您的意思是string.isnullorwhitespace测试或查找官方文档是否太难?(). 简短回答:不,一串空格不是null或空的。你们错过了要点。是的,您可以使用
IsNullOrEmpty
,但是OP不明白为什么需要第一次调用来避免
NullReferenceException
。了解这一点比切换到其他方法调用要重要得多,因为他被告知这样做。@madth3我想他实际上是在问是否可以省略第一个检查:
!string.IsNullOrEmpty(cell.Text)
,而不是
string.IsNullOrEmpty()
是否检查空格,但必须将问题读几遍。@ntziolis你是对的,我误解了要删除的行。我仍然认为,尝试一下并查看官方文件并不难。是的,但这并不能回答问题。显然,OP对if语句表达式的计算方式感到困惑。如果单元格是文本框,则该属性永远不会为null。这也可以通过
cell.Text!=null&&cell.Text.Trim()!=“”
如果您还有.NET<.NET 4.0。注意:单元格是使用WebDriver获取的html表格单元格内容(但这不是问题的重点),我认为在这种情况下扩展方法并不更好。根据经验,“不要接受null作为有效的扩展方法参数”。其他程序员可能会将此解释为“文本必须有一个值,因为可以调用一个方法”。