![]() |
文武双全的毛衣 · 全国助人为乐模范候选人 - 中华人民共和国国防部· 6 月前 · |
![]() |
发财的饭卡 · 难道杨鼎新的狂妄是得到了聂卫平真传吗? - 知乎· 1 年前 · |
![]() |
任性的小马驹 · 广州市科学技术局关于发布2023年度市重点研 ...· 1 年前 · |
![]() |
面冷心慈的水煮鱼 · 哥布林洞窟百度云 - 百度· 1 年前 · |
![]() |
淡定的充电器 · 曼岛tt时间表2023 - 抖音· 2 年前 · |
在PowerShell中,有没有内置的
IsNullOrEmpty
-like函数来检查字符串是空的还是空的?
到目前为止,我找不到它,如果有内置的方法,我不想为此编写函数。
发布于 2012-12-06 15:48:30
您可以使用
IsNullOrEmpty
静态方法:
[string]::IsNullOrEmpty(...)
发布于 2012-12-07 00:21:38
你们把事情搞得太复杂了。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
发布于 2012-12-06 16:30:54
除了
[string]::IsNullOrEmpty
之外,为了检查null或空,您还可以显式地或在布尔表达式中将字符串强制转换为布尔值:
$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
![]() |
文武双全的毛衣 · 全国助人为乐模范候选人 - 中华人民共和国国防部 6 月前 |
![]() |
发财的饭卡 · 难道杨鼎新的狂妄是得到了聂卫平真传吗? - 知乎 1 年前 |
![]() |
面冷心慈的水煮鱼 · 哥布林洞窟百度云 - 百度 1 年前 |
![]() |
淡定的充电器 · 曼岛tt时间表2023 - 抖音 2 年前 |