在PowerShell中,是否有内置的类似于IsNullOrEmpty
的函数来检查字符串是否为null或为空?
我目前还没有找到相关函数,如果有内置方法,我就不想自己写一个函数了。
在PowerShell中,是否有内置的类似于IsNullOrEmpty
的函数来检查字符串是否为null或为空?
我目前还没有找到相关函数,如果有内置方法,我就不想自己写一个函数了。
你们让这个问题变得太复杂了。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
IsNullOrWhitespace()
。但是在使用PowerShell脚本11年后,我发现几乎不需要进行字符串测试。 :-) - Keith Hill$str1=$false
或者
$str1=@(0)
- dmitry0
,但如果它不应该有,那么这不一定是_你的方法_需要关心的。(但也可能潜在的错误足够严重,值得额外的防御。) - John Neuhaus您可以使用IsNullOrEmpty
静态方法:
[string]::IsNullOrEmpty(...)
!
符号。它只适用于较新版本的 PowerShell。 !
是 -not
的别名。 - Kellen Stuart除了使用[string]::IsNullOrEmpty
来检查空或空字符串之外,您还可以将字符串显式地转换为布尔值,或在布尔表达式中使用:
$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
If
子句在内部将括号内的所有内容转换为单个布尔值,这意味着if($string){对于非空且非null的情况要执行的操作}
或if(!$string){对于空或null的情况要执行的操作}
,无需显式转换[bool]
即可。 - Ch.IdeaValidateNotNullOrEmpty
对其进行验证,以下是一个例子:
若参数在函数中,你可以使用ValidateNotNullOrEmpty
对其进行验证,以下是一个例子:
Function Test-Something
{
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$UserName
)
#stuff todo
}
就我个人而言,我不认为一个只包含空格($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'}
非空
PowerShell 2.0替代 [string]::IsNullOrWhiteSpace()
的方法是 string -notmatch "\S"
("\S" 表示任何非空白字符)
> $null -notmatch "\S"
True
> " " -notmatch "\S"
True
> " x " -notmatch "\S"
False
性能非常接近:
> Measure-Command {1..1000000 |% {[string]::IsNullOrWhiteSpace(" ")}}
TotalMilliseconds : 3641.2089
> Measure-Command {1..1000000 |% {" " -notmatch "\S"}}
TotalMilliseconds : 4040.8453
# Test for $null or '' (empty string).
# Equivalent of: [string]::IsNullOrEmpty($str)
$str -like ''
# Test for $null or '' or all-whitespace.
# Equivalent of: [string]::IsNullOrWhitespace($str)
$str -notmatch '\S'
$str
可以是一个集合(数组),请使用底部的类型不可知解决方案。-like
运算符会隐式将LHS强制转换为字符串,由于[string] $null
生成空字符串,因此'' -like ''
和$null -like ''
都返回$true
。-match
/ -notmatch
运算符作为仅字符串运算符,将其LHS操作数强制转换为字符串,在此转换中,$null
被视为''
。
\S
是一个正则表达式转义序列,用于匹配任何非空白字符(它是\s
的否定形式)。
-match
/ -notmatch
默认执行子字符串匹配(并且始终只返回一个匹配项),因此如果\S
匹配,则意味着至少存在一个非空白字符。
注意:
由于PowerShell的动态类型,您可能无法提前知道给定变量中存储的值的类型。
虽然上述技术对于$null
、[string]
实例和其他不可枚举类型的工作是可预测的,但可枚举的值(除了字符串)可能会产生令人惊讶的结果,因为如果-like
和-notmatch
的左侧是可枚举的(宽泛地说:集合),操作将应用于每个元素,而不是返回单个布尔值,而是返回匹配元素的子数组。
在条件语句的上下文中,将数组强制转换为布尔值的方式有些反直觉;如果数组只有一个元素,则该元素本身被强制转换为布尔值;如果有两个或更多元素,则数组总是被强制转换为$true
,而不考虑元素的值 - 请参阅this answer的底部部分。例如:
# -> 'why?', because @('', '') -like '' yields @('', ''), which
# - due to being a 2-element array - is $true
$var = @('', '')
if ($var -like '') { 'why?' }
# Test for $null or '' (empty string) or any stringified value being ''
# Equivalent of: [string]::IsNullOrEmpty($var)
[string] $var -eq ''
# Test for $null or '' or all-whitespace or any stringified value being ''
# Equivalent of: [string]::IsNullOrWhitespace($var)
([string] $var).Trim() -eq ''
我有一个PowerShell脚本需要在一台过于陈旧的计算机上运行,该计算机没有[String] :: IsNullOrWhiteSpace()函数,因此我编写了自己的函数。
function IsNullOrWhitespace($str)
{
if ($str)
{
return ($str -replace " ","" -replace "`t","").Length -eq 0
}
else
{
return $TRUE
}
}
.Trim()
代替列出空格类型?这样就可以写成$str.Trim().Length -eq 0
。 - HackSlash$str -notmatch '\S'
应该足够覆盖所有类型的空白符,而不仅仅是空格和制表符。 - mklement0# cases
$x = null
$x = ''
$x = ' '
# test
if ($x -and $x.trim()) {'not empty'} else {'empty'}
or
if ([string]::IsNullOrWhiteSpace($x)) {'empty'} else {'not empty'}
IsNullOrWhitespace
情况的问题,在 PowerShell 7.1 及更高版本中,我们可以使用 null-conditional member operator 优雅地检查字符串是否为 null 或空格,而无需自己首先检查该字符串不是 $null
,同时避免使用 [string]::IsNullOrWhitespace(string)
。
使用 Keith 的答案中的示例Note: You can also do this with PowerShell 7.0 if you enable the
PSNullConditionalOperators
experimental feature:Enable-ExperimentalFeature -Name PSNullConditionalOperators
$str3
(为了清晰起见,假装三元运算符在7.0之后也不存在):$str3 = ' '
if ( ${str3}?.Trim() ) {
'not empty or whitespace'
} else {
'empty or whitespace'
}
empty or whitespace
$str3
是非空值,则仅调用 .Trim()
方法;否则返回 $null
。
?
可以作为变量名的一部分。这就是为什么我们必须先消除变量名的歧义,然后再应用条件访问运算符,例如:${str3}
。
${str3}?.Trim() ? 'not empty or whitespace' : 'empty or whitespace'
三元运算符是基本条件语句的简化版if/then/else
。在这里,我不想过多地涉及它的细微差别,但可以理解为“如果单独问号?
左侧为真,则执行问号右侧的内容,否则执行冒号:
后面的内容。”
您可以在PowerShell文档中了解更多关于三元运算符的信息。
String.IsNullOrEmpty
吗? - Jon Skeet