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

[玩转系统] PowerShell isNullOrEmpty 和空白

作者:精品下载站 日期:2024-12-14 22:04:54 浏览:12 分类:玩电脑

PowerShell isNullOrEmpty 和空白


在 PowerShell 中,.Net Framework System.String 类的内置静态方法 IsNullOrEmpty 可用于确定字符串变量是否为 null 或为空。

使用 IsNullOrEmpty 方法功能并将其与其他验证方法相结合,您可以编写无错误的 PowerShell 脚本。

.Net Framework System.String 类的 IsNullOrWhiteSpace 内置静态方法可有效地用于确定字符串变量是否仅包含空字符或空白字符。

在本文中,我们将讨论如何使用 PowerShell isNullOrEmpty 方法检查字符串是否为空或 null,以及如何使用 IsNullOrWhiteSpace 方法检查字符串是否包含 null 或空格字符。

检查字符串是否为 Null 或空

使用 IsNullOrEmpty 方法检查字符串变量是否为 null 或空,请遵循以下语法:

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

在上面的 PowerShell 脚本中,我们定义了一个变量 $tempVariable。要检查变量是否为 null 或空,if 语句使用静态方法 IsNullOrEmpty 将变量作为输入并评估字符串变量是否为 null 或空。

上述 PowerShell 脚本的输出确定变量具有任何值或为 null,并使用 Write-Host cmdlet 打印输出。

PS C:\> $tempVariable                                                                                                   

PS C:\> if([string]::IsNullOrEmpty($tempVariable)) 
{ 
    Write-Host "The string is null or empty"
} 
else 
{  Write-Host "asd"
}  

The string is null or empty

PS C:\>

让我们看几个 PowerShell 中 IsNullOrEmpty 的更实际的例子。

PowerShell 变量 Null 或空检查用户输入

我们举个例子,使用 do-while 循环来验证用户输入是否为 null 或为空,语法如下:

do 
{ 
   $username = Read-Host "Please enter a user name" 
} while ([string]::IsNullOrEmpty($username))  

在上面的 PowerShell 脚本中,我们使用了 do-while 循环来读取用户有关用户名的输入,并确定变量是否为 null 或为空。如果变量为空或空,它将再次要求用户“请输入用户名”。

上述用于检查变量是否为 null 或空的 PowerShell 脚本的输出是:

[玩转系统] PowerShell isNullOrEmpty 和空白

PowerShell 如果函数中变量为 Null 或为空

要确保函数中的强制参数并检查变量是否不为空或 null,请使用以下语法:

function Validate-String {
    param (
        [string]$inputString
    )
    
    if ([string]::IsNullOrEmpty($inputString)) {
        Write-Error "The input string parameter cannot be null or empty"
        return
    }
    
    # Continue processing the input string parameter
}

在上面的PowerShell脚本中,我们创建了一个函数Validate-String。该函数采用输入参数$inputString。在函数中,它使用 IsNullOrEmpty 静态方法 System.String 类检查变量是否为空或 null。

上述 PowerShell 脚本的输出验证函数中的变量是否为 null 或为空。


PS C:\> Validate-String -inputString ""
Validate-String : The input string parameter cannot be null or empty
At line:1 char:1
+ Validate-String -inputString ""
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Validate-String

检查字符串是否为空或空白字符

要检查字符串是否仅包含空字符或空白字符,请使用 IsNullOrWhiteSpace 方法,并遵循以下语法:

$userInput = "shelladmin"
if ([string]::IsNullOrWhiteSpace($userInput )) {
    Write-Host "The string is null or contains only whitespace characters"
}
else {
 Write-Host "The string does not contains null or whitespace characters"
}

在上面的 PowerShell 脚本中,if 语句使用 IsNullOrWhiteSpace 方法来识别字符串变量是否包含 null 或仅包含空格字符并打印输出。

上面的 PowerShell 脚本验证字符串变量,并且由于它不为 null 或包含空格字符,因此它返回的输出为:

The string does not contains null or whitespace characters

常见错误和最佳实践

null 和空字符串之间存在差异,因此请使用正确的方法来验证输入。使用 -eq $null-ne $null 检查可以为 null 的非字符串数据类型中的 null 值。

使用 IsNullOrWhileSpaceIsNullOrEmpty 方法根据要求验证字符串变量。

结论

希望上面关于如何使用PowerShell IsNullOrEmpty方法识别null或空字符串的文章对您有所帮助。

使用 IsNullOrWhiteSpace() 方法验证字符串是否包含空字符或空白字符。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯