首页 文章

如何在PowerShell中检查字符串是空还是空?

提问于
浏览
311

在PowerShell中是否有内置的 IsNullOrEmpty -like函数来检查字符串是空还是空?

到目前为止我找不到它,如果有内置方式,我不想为此编写函数 .

10 回答

  • -1

    除了 [string]::IsNullOrEmpty 之外,为了检查null或empty,您可以显式地或在布尔表达式中将字符串强制转换为布尔值:

    $string = $null
    [bool]$string
    if (!$string) { "string is null or empty" }
    
    $string = ''
    [bool]$string
    if (!$string) { "string is null or empty" }
    
    $string = 'something'
    [bool]$string
    if ($string) { "string is not null or empty" }
    

    输出:

    False
    string is null or empty
    
    False
    string is null or empty
    
    True
    string is not null or empty
    
  • 4

    请注意, "if ($str)""IsNullOrEmpty" 测试在所有实例中都不起作用:$ str = 0的赋值对两者都产生错误,并且根据预期的程序语义,这可能会产生意外 .

  • 15

    检查长度 . 如果对象存在,则它将具有长度 .

    空对象没有长度,不存在,无法检查 .

    String对象有一个长度 .

    问题是:IsNull或IsEmpty,而不是IsNull或IsEmpty或IsWhiteSpace

    #Null
    $str1 = $null
    $str1.length
    ($str1 | get-member).TypeName[0]
    # Returns big red error
    
    #Empty
    $str2 = ""
    $str2.length
    ($str2 | get-member).TypeName[0]
    # Returns 0
    
    ## Whitespace
    $str3 = " "
    $str3.length
    ($str3 | get-member).TypeName[0]
    ## Returns 1
    
  • 8

    我有一个PowerShell脚本,我必须在计算机上运行,所以它没有[String] :: IsNullOrWhiteSpace(),所以我写了自己的 .

    function IsNullOrWhitespace($str)
    {
        if ($str)
        {
            return ($str -replace " ","" -replace "`t","").Length -eq 0
        }
        else
        {
            return $TRUE
        }
    }
    
  • 32

    以纯PowerShell方式实现此目的的另一种方法是执行以下操作:

    ("" -eq ("{0}" -f $val).Trim())
    

    这会成功评估null,空字符串和空格 . 我将传递的值格式化为空字符串以处理null(否则,在调用Trim时,null将导致错误 . )然后只评估与空字符串的相等性 . 我想我仍然更喜欢IsNullOrWhiteSpace,但是如果你正在寻找另一种方法,这将是有效的 .

    $val = null    
    ("" -eq ("{0}" -f $val).Trim())
    >True
    $val = "      "
    ("" -eq ("{0}" -f $val).Trim())
    >True
    $val = ""
    ("" -eq ("{0}" -f $val).Trim())
    >True
    $val = "not null or empty or whitespace"
    ("" -eq ("{0}" -f $val).Trim())
    >False
    

    编辑:在无聊的情况下,我玩了一些,并使其更短(虽然更神秘)

    !!(("$val").Trim())
    

    要么

    !(("$val").Trim())
    

    取决于你想要做什么

  • 1

    您可以使用 IsNullOrEmpty 静态方法:

    [string]::IsNullOrEmpty(...)
    
  • 1

    你们这样做太难了 . PowerShell处理这个非常优雅,例如:

    > $str1 = $null
    > if ($str1) { 'not empty' } else { 'empty' }
    empty
    
    > $str2 = ''
    > if ($str2) { 'not empty' } else { 'empty' }
    empty
    
    > $str3 = ' '
    > if ($str3) { 'not empty' } else { 'empty' }
    not empty
    
    > $str4 = 'asdf'
    > if ($str4) { 'not empty' } else { 'empty' }
    not empty
    
    > if ($str1 -and $str2) { 'neither empty' } else { 'one or both empty' }
    one or both empty
    
    > if ($str3 -and $str4) { 'neither empty' } else { 'one or both empty' }
    neither empty
    
  • 489

    如果它是函数中的参数,您可以使用 ValidateNotNullOrEmpty 验证它,如您在此示例中所示:

    Function Test-Something
    {
        Param(
            [Parameter(Mandatory=$true)]
            [ValidateNotNullOrEmpty()]
            [string]$UserName
        )
    
        #stuff todo
    }
    
  • 350
    # cases
    $x = null
    $x = ''
    $x = ' '
    
    # test
    if ($x -and $x.trim()) {'not empty'} else {'empty'}
    or
    if ([string]::IsNullOrWhiteSpace($x)) {'empty'} else {'not empty'}
    
  • 0

    就个人而言,我不接受空格($ STR3)为“非空” .

    当一个只包含空格的变量被传递给一个参数时,通常会错误的是参数值可能不是'$ null',而不是说它可能不是一个空格,一些删除命令可能会删除一个根文件夹而不是一个子文件夹如果子文件夹名称是“空格”,所有的理由是在很多情况下不接受包含空格的字符串 .

    我发现这是实现它的最佳方法:

    $STR1 = $null
    IF ([string]::IsNullOrWhitespace($STR1)){'empty'} else {'not empty'}
    

    $STR2 = ""
    IF ([string]::IsNullOrWhitespace($STR2)){'empty'} else {'not empty'}
    

    $STR3 = " "
    IF ([string]::IsNullOrWhitespace($STR3)){'empty !! :-)'} else {'not Empty :-('}
    

    空! :-)

    $STR4 = "Nico"
    IF ([string]::IsNullOrWhitespace($STR4)){'empty'} else {'not empty'}
    

    不是空的

相关问题