How can I check if a string is null or empty in PowerShell?

后端 未结 11 1058
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 04:49

Is there a built-in IsNullOrEmpty-like function in order to check if a string is null or empty, in PowerShell?

I could not find it so far and if there i

11条回答
  •  孤街浪徒
    2020-12-02 04:56

    An extension of the answer from Keith Hill (to account for whitespace):

    $str = "     "
    if ($str -and $version.Trim()) { Write-Host "Not Empty" } else { Write-Host "Empty" }
    

    This returns "Empty" for nulls, empty strings, and strings with whitespace, and "Not Empty" for everything else.

提交回复
热议问题