当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 PowerShell 中检查字符串是否为 Null 或空 [4 种方法]

作者:精品下载站 日期:2024-12-14 05:27:25 浏览:11 分类:玩电脑

在 PowerShell 中检查字符串是否为 Null 或空 [4 种方法]


[玩转系统] 在 PowerShell 中检查字符串是否为 Null 或空 [4 种方法]

使用 -not-eq 运算符

使用 -not 运算符检查字符串是否为 null 或 PowerShell 中的空。

使用 -not 和 -eq 运算符:

$string=$null
if (-not $string -or $string -eq '') {
    Write-Host "The provided string is null or empty."
}
else{
    Write-Host "The provided string is not null or empty."
}

输出 :

The provided string is null or empty.

首先,我们声明 $string 变量并使用 $null 对其进行初始化。 $null是什么?它是 PowerShell 中的一个自动变量,表示 NULL 值。现在,什么是NULL?我们可以将其视为空值或未知值。任何变量都将是Null,直到我们为它分配任何对象或值。

接下来,我们使用带有 -not-eq 运算符的 if 语句来检查 $string 是否为 null 或空。这里,-not 是一个逻辑非运算符,用于否定其后面的表达式的结果。因此,在上面的代码片段中,-not 否定了 $string 的结果;由于 $stringnull,即 PowerShell 中的 False 值,因此 -not 会将其转换为正确

-eq 运算符,表示等于。它用于比较两个值以确定它们是否相等。接下来,我们将 $string 与用 '' 表示的空字符串进行比较。如果条件满足,则执行if语句;否则,else 块将是。

这里,我们使用 -or 来表示逻辑或,这将有助于执行 if 块 if $string -eq '' or -not $string 满意。最后,我们使用 Write-Host 在主机(我们的 PowerShell 控制台)上显示自定义消息。同样,我们可以使用 -eq 运算符,如下所示。

使用 -eq 运算符检查字符串是否为 null 或 PowerShell 中的空。

使用 -eq 运算符:

$string=$null
if ($string -eq '' -or $string -eq $null) {
    Write-Host "The provided string is null or empty."
}
else{
    Write-Host "The provided string is not null or empty."
}

输出 :

The provided string is null or empty.

此代码与前面的示例类似,但我们使用 -eq 运算符来比较两个值以确定它们是否相等。我们将 $string 与用 ''$null 表示的空字符串进行比较。如果条件满足,则执行if语句;否则,else 块将是。

使用 IsNullOrEmpty() 方法

使用 IsNullOrEmpty() 方法检查 PowerShell 中的字符串是否为 null 或空。

使用 IsNullOrEmpty() 方法:

$string=''
if ([string]::IsNullOrEmpty($string)) {
    Write-Host "The provided string is null or empty."
}
else{
    Write-Host "The provided string is not null or empty."
}

输出 :

The provided string is null or empty.

对于上面的代码片段,我们使用string类的IsNullOrEmpty()方法来确定$string是否为空或null。该方法采用一个 string 类型的参数,并根据给定的 $string 返回一个布尔值。如果 $stringnull 或空字符串,结果将为 True,这将导致执行 if堵塞;否则,它将是 False,并且 else 块将被执行。

如果您还想在测试变量时包含空格,可以使用以下解决方案。

使用 IsNullOrEmpty()IsNullOrWhitespace() 方法检查 PowerShell 中的字符串是否为 null 或空。

使用 ISNullOrEmpty() 和 IsNullOrWhitespace() 方法:

$string='    '
if ([string]::IsNullOrEmpty($string) -or [string]::IsNullOrWhitespace($string) ) {
    Write-Host "The provided string is null or empty."
}
else{
    Write-Host "The provided string is not null or empty."
}

输出 :

The provided string is null or empty.

在上面的代码围栏中,IsNullOrEmpty() 检查 $string 是否为空或 null,而 IsNullOrWhitespace() 则查找 $string null 或有空格。如果这两个方法中的任何一个返回 True,则 if 块将运行;否则,else 块。

-notmatch 运算符与正则表达式结合使用

-notmatch 运算符与正则表达式结合使用,检查指定字符串在 PowerShell 中是否为 null 或空。

将 -notmatch 运算符与正则表达式一起使用:

$string=''
if ($string -notmatch '\S') {
    Write-Host "The provided string is null or empty."
}
else{
    Write-Host "The provided string is not null or empty."
}

输出 :

The provided string is null or empty.

在这里,我们使用 -notmatch 运算符和正则表达式 \S 来检查 $string 是否为 null或空。这里,\S 表示任何非空白字符。如果 $string 包含任何非空白字符,则 -notmatch 运算符将返回 Falseelse 块将被处决;否则,Write-Host 将在 if 块内运行。

使用类型转换

使用类型转换(也称为动态转换)来检查给定字符串在 PowerShell 中是否为 null 或空。

使用类型转换:

$string=$null
[bool]$string
$string=''
[bool]$string
$string='Hello'
[bool]$string

输出 :

False
False
True

在这里,我们在运行时将 $string 转换为 bool 类型,以获取布尔值来了解 $string 变量是否为空或 >空。如果$string为空或null,上面的代码将显示False;否则,True

这就是如何在 PowerShell 中检查 String 是否为 null 或 Empty。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯