网站搜索

在 PowerShell 中检查数组是否包含元素 [5 种方法]


1. 概述

在本文中,我们将看到在 PowerShell 中使用 -contains 运算符、Contains() 方法、Where-Object 检查数组是否包含元素的不同方法 cmdlet、-in 运算符、indexOf() 方法和 LINQ。

2. 问题陈述简介

我们得到一个数组和元素。 我们的目标是检查数组是否包含该元素。

例如,给定一个数组 [PowerShell", "Java", "PHP"],我们想要检查字符串 "Java" 是否是该数组的元素。这个简单的查询提出了一系列重要的考虑因素,例如区分大小写和处理不同数据类型的能力。

3.使用-Contains运算符

-contains 运算符在数组中执行线性搜索,如果找到元素则返回$true,否则返回$false。< 这是最直接、最简单的方法。

让我们借助示例来了解 -Contains 运算符:

使用-包含运算符:

$array = @("PowerShell", "Java", "PHP")
$element= "Java"
if ($array -Contains $element) {
    Write-Host "The array contains the '$element'."
} else {
    Write-Host "The array does not contain the '$element'."
}

输出 :

The array contains the 'Java'.

此运算符不区分大小写,这意味着 Java、JAVA 和 java 是相同的值。

在 if-else 块内,我们使用 Write-Hostcmdlet 在 PowerShell 控制台上打印自定义消息,让用户知道是否在$数组。

4.使用-in运算符

PowerShell v3.0 中引入的 in 运算符是 -contains 的替代语法。

如果元素位于 $array 中,此操作将返回 $true。 它在语法上不同,但功能上与 -contains 相似。

使用 -in 运算符:

$array = @("PowerShell", "Java", "PHP")
$element= "Java"
if ($element -in $array){
    Write-Host "The array contains the '$element'."
} else {
    Write-Host "The array does not contain the '$element'."
}

输出 :

The array contains the 'Java'.

由于这类似于 -contains 运算符,它也不区分大小写,这意味着 Java、JAVA 和 java 是相同的值。

5. 将 IndexOf() 方法与 -ne 运算符一起使用

indexOf() 方法返回元素的索引(如果存在)。如果指定元素的索引不为-1,则它在数组中。

让我们通过示例来看看:

将 IndexOf() 方法与 -ne 运算符一起使用:

$array = "PowerShell", "Java", "PHP"
$element= "PHP"
if ($array.IndexOf($element) -ne -1) {
    Write-Host "The array contains the '$element'."
} else {
    Write-Host "The array does not contain the '$element'."
}

输出 :

The array contains the 'PHP'.

因此,在这里,IndexOf() 方法接受一个值并返回其在 $array 中的索引;如果未找到指定元素,则返回 -1。

这种方法区分大小写,意味着 PHP 和 php 是两个不同的元素。

6. 使用.Contains()方法

检查元素是否存在的另一种方法是使用 .Contains() 方法。 .Contains() 方法检查数组中是否存在元素。

使用 .Contains() 方法:

$array = @("PowerShell", "Java", "PHP")
$element= "Java"
if ($array.Contains($element)){
    Write-Host "The array contains the '$element'."
} else {
    Write-Host "The array does not contain the '$element'."
}

输出 :

The array contains the 'Java'.

在这里,我们使用了 .Contains() 方法,该方法的作用与 -Contains 运算符相同,并确定 $element 是否存在于$array,但它区分大小写。

7. 使用带有脚本块的Where-Object Cmdlet

Where-Object cmdlet 提供了一种更灵活的方法来搜索数组。默认情况下不区分大小写。我们可以使用 -ceq 运算符进行区分大小写的搜索。

将Where-Object Cmdlet 与脚本块结合使用:

$array = @("PowerShell", "Java", "PHP")
$element= "Java"
if ($array | Where-Object {$_ -eq $element}){
    Write-Host "The array contains the '$element'."
} else {
    Write-Host "The array does not contain the '$element'."
}

输出 :

The array contains the 'Java'.

让我们更多地了解 $array | Where-Object {$_ -eq $element}:

  • $array
  • |
  • Where-Object
  • { $_ -eq $element }
    • $_
    • -eq
  • if 条件:if 语句计算Where-Object cmdlet 的输出。如果Where-Object 在$array 中找到与$element 匹配的任何元素,它将返回这些元素,并且if 条件被视为$true。如果没有元素匹配,则 if 条件为 $false。

8.使用LINQ

LINQ 查询提供了在 PowerShell 中处理集合的强大且高效的方法。

使用 LINQ:

[array]$array = "apple", "banana", "cherry"
$item = "banana"
if ([System.Linq.Enumerable]::Contains($array, $item)) {
    "The array contains the item."
} else {
    "The item is not in the array."
}

如果在数组中找到该元素,此方法将返回 $true。LINQ 查询因其性能而闻名,对于大型数据集特别有用。将其用于小数据集可能会产生开销。

9. 检查元素是否以数组中的子字符串开头

我们可以使用 -like 运算符来检查数组是否包含具有特定模式的元素。

这是一个例子:

使用 Like 运算符:

$array = @("PowerShell", "Java", "PHP")
$element= "J*"
if ($array | Where-Object {$_ -like $element}){
    Write-Host "The array contains the '$element'."
} else {
    Write-Host "The array does not contain the '$element'."
}

输出 :

The array contains the 'J*'.

10. 复杂对象的自定义比较

在处理复杂对象时,我们可能需要执行自定义操作,例如根据 PSCustomObject 的属性比较数组。

复杂类型的自定义比较:

$person1 = [PSCustomObject]@{Name="John"; Age=30}
$person2 = [PSCustomObject]@{Name="Jane"; Age=25}
$myArray = $person1, $person2
$searchName = "Jane"
$result = $myArray | Where-Object { $_.Name -eq $searchName }

此脚本检查自定义对象数组中是否存在具有特定 Name 属性值的对象。

11. 使用数组中的不同数据类型

在 PowerShell 中,数组是一种可以保存项目集合的数据结构。与某些要求数组中的所有元素均为同一类型的编程语言不同,PowerShell 数组用途广泛,可以包含混合的数据类型。

混合数组示例:

具有混合数据类型的数组:

$myArray = 42, "PowerShell", $true, 19.99

我们可以使用 -contains 运算符、.Contains() 方法或 Where-Object cmdlet。这些的工作原理与我们已经讨论过的类似。

请注意,在检查数组是否包含任何元素时,它还会检查其数据类型。

混合数据类型:

$myArray = 42, "PowerShell", $true, 19.99
$item = "42"
$result = $myArray.Contains($item)

这里,$result 将为 $false,因为“42”(字符串)与 42(整数)不是同一类型。

如果我们将 42 更改为整数,它将返回 $true。

混合数据类型:

$myArray = 42, "PowerShell", $true, 19.99
$item = 42
$result = $myArray.Contains($item)

在这里,$result 现在将是 $true。

12. 结论

在本文中,我们探索了多种方法来检查 PowerShell 中的数组是否包含特定元素。

这些方法的范围从简单的 -contains 和 -in 运算符到更复杂的 Where-Object cmdlet 和 LINQ查询。每种方法都有其独特的特点和最佳用例。

-contains 和 -in 运算符的简单性使其成为快速检查的理想选择,并且因其不区分大小写的特性而特别有用。为了进行更精确、区分大小写的检查,可以使用 .Contains() 方法和indexOf() 提供了一种直接方法,后者提供有关元素在数组中位置的附加信息。

相关文章