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

本文翻译自:How can I check if a string is null or empty in PowerShell?

Is there a built-in IsNullOrEmpty -like function in order to check if a string is null or empty, in PowerShell? 在PowerShell中是否有内置的IsNullOrEmpty函数来检查字符串是空还是空?

I could not find it so far and if there is a built-in way, I do not want to write a function for this. 到目前为止我找不到它,如果有内置方式,我不想为此编写函数。


#1楼

参考:https://stackoom.com/question/ve2s/如何在PowerShell中检查字符串是空还是空


#2楼

您可以使用IsNullOrEmpty静态方法:

[string]::IsNullOrEmpty(...)

#3楼

In addition to [string]::IsNullOrEmpty in order to check for null or empty you can cast a string to a Boolean explicitly or in Boolean expressions: 除了[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" }

Output: 输出:

False
string is null or empty

False
string is null or empty

True
string is not null or empty

#4楼

You guys are making this too hard. 你们这样做太难了。 PowerShell handles this quite elegantly eg: 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

#5楼

If it is a parameter in a function, you can validate it with ValidateNotNullOrEmpty as you can see in this example: 如果它是函数中的参数,则可以使用ValidateNotNullOrEmpty对其进行ValidateNotNullOrEmpty ,如下例所示:

Function Test-Something
{
    Param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$UserName
    )

    #stuff todo
}

#6楼

Personally, I do not accept a whitespace ($STR3) as being 'not empty'. 就个人而言,我不接受空格($ STR3)为“非空”。

When a variable that only contains whitespaces is passed onto a parameter, it will often error that the parameters value may not be '$null', instead of saying it may not be a whitespace, some remove commands might remove a root folder instead of a subfolder if the subfolder name is a "white space", all the reason not to accept a string containing whitespaces in many cases. 当一个只包含空格的变量被传递给一个参数时,通常会错误的是参数值可能不是'$ null',而不是说它可能不是空格,一些删除命令可能会删除一个根文件夹而不是一个子文件夹如果子文件夹名称是“空格”,则在很多情况下不接受包含空格的字符串的所有理由。

I find this is the best way to accomplish it: 我发现这是实现它的最佳方式:

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

Empty

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

Empty

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

Empty!! 空! :-) :-)

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

Not empty 不是空的

你可能感兴趣的:(如何在PowerShell中检查字符串是空还是空?)