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

[玩转系统] PowerShell -包含运算符[带有示例]

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

PowerShell -包含运算符[带有示例]


作为一名 PowerShell 开发人员,您应该知道如何在 PowerShell 中使用 -contains 运算符。在本教程中,我将通过示例解释如何使用 PowerShell -contains 运算符

PowerShell 中的 -contains 运算符是什么?

PowerShell 中的 -contains 运算符用于确定集合(例如数组或列表)中是否存在指定的项目。它返回一个布尔值:如果找到该项目,则返回 $true;如果未找到,则返回 $false

当您需要检查集合中是否存在元素而无需手动迭代每个项目时,此运算符特别有用。

-contains 运算符的语法

-contains 运算符的语法是:

<collection> -contains <item>
  • <collection>:这是您正在搜索的数组或集合。
  • <item>:这是您在集合中检查的项目。

读取 PowerShell Like 运算符

PowerShell - 包含运算符示例

现在,让我们通过检查 PowerShell 中 -contains 运算符的几个示例来了解更多信息。

我们将从一个基本示例开始

示例 1:基本示例

以下是如何在 PowerShell 中使用 -contains 运算符的基本示例。

# Define an array of numbers
$numbers = 1, 2, 3, 4, 5

# Check if the number 3 is in the array
if ($numbers -contains 3) {
    Write-Output "The array contains the number 3."
} else {
    Write-Output "The array does not contain the number 3."
}

在此示例中,脚本检查 $numbers 数组中是否存在数字 3。由于 3 确实在数组中,因此输出将是:

The array contains the number 3.

我执行了上面的脚本,您可以在下面的屏幕截图中看到确切的输出:

[玩转系统] PowerShell -包含运算符[带有示例]

查看在 PowerShell 中检查字符串是否包含空格

示例 2:将 -contains 与字符串一起使用

让我向您展示另一个例子。在这里,我将向您展示如何在 PowerShell 中对字符串使用 -contains 运算符。

-contains 运算符也可以与字符串数组一起使用。但需要注意的是,-contains 并不执行子字符串匹配;它只查找完全匹配的内容。

# Define an array of strings
$fruits = "apple", "banana", "cherry"

# Check if the array contains the string "banana"
if ($fruits -contains "banana") {
    Write-Output "The array contains 'banana'."
} else {
    Write-Output "The array does not contain 'banana'."
}

该脚本将输出:

The array contains 'banana'.

我使用 VS Code 执行上述脚本后,您可以在下面的屏幕截图中看到输出。

[玩转系统] PowerShell -包含运算符[带有示例]

在 PowerShell 中查看 -and 运算符

示例 3:区分大小写

默认情况下,-contains 运算符不区分大小写。但是,如果您需要执行区分大小写的比较,可以使用 -ccontains 运算符,如下例所示。

# Define an array of strings
$fruits = "Apple", "Banana", "Cherry"

# Check if the array contains the string "banana" (case-sensitive)
if ($fruits -ccontains "banana") {
    Write-Output "The array contains 'banana'."
} else {
    Write-Output "The array does not contain 'banana'."
}

由于 -ccontains 区分大小写,并且“banana”(带有小写“b”)没有完全匹配,因此输出将是:

The array does not contain 'banana'.

您可以在下面的屏幕截图中看到确切的输出。

[玩转系统] PowerShell -包含运算符[带有示例]

示例 4:使用 -contains 运算符检查数组中的对象

现在,让我向您展示一个在 PowerShell 中使用 -contains 运算符的高级示例。

-contains 运算符还可用于检查对象数组中的对象。

这是一个完整的例子。

# Define an array of custom objects
$people = @(
    [PSCustomObject]@{ Name = "Alice"; Age = 30 },
    [PSCustomObject]@{ Name = "Bob"; Age = 25 },
    [PSCustomObject]@{ Name = "Charlie"; Age = 35 }
)

# Define a person object to search for
$searchPerson = [PSCustomObject]@{ Name = "Bob"; Age = 25 }

# Check if the array contains the specified person object
if ($people -contains $searchPerson) {
    Write-Output "The array contains the specified person."
} else {
    Write-Output "The array does not contain the specified person."
}

在此示例中,脚本创建一个表示人员的自定义对象数组,然后检查该数组中是否存在特定的人员对象。输出将是:

The array contains the specified person.

结论

PowerShell 中的 -contains 运算符检查集合中项目是否存在。在本教程中,我通过四个不同的示例解释了如何使用 PowerShell -contains 运算符

还有一些问题吗?欢迎在下面发表评论。

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

取消回复欢迎 发表评论:

关灯