[玩转系统] PowerShell 检查列表是否包含字符串
作者:精品下载站 日期:2024-12-14 05:29:47 浏览:14 分类:玩电脑
PowerShell 检查列表是否包含字符串
使用 -Contains
运算符
使用-Contains
运算符检查列表是否包含PowerShell中指定的字符串。
使用-包含运算符:
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
$searchString = "Wednesday"
$list -Contains $searchString
输出 :
True
在上面的示例中,我们使用以 @()
表示的数组运算符来创建一个列表,并将其存储在 $list
变量中。接下来,我们使用要在 $list
中搜索的字符串值初始化 $searchString
变量。
最后,我们使用 -Contains
运算符来确定 $list
是否包含 $searchString
。如果是,我们将得到 True 作为输出;否则,False
。请注意,-Contains
运算符不区分大小写,这意味着它将处理 "Wednesday"
、"wednesday"
和 "WeDnesDay ” 相同。
在 PowerShell 中,-Contains
和 -iContains
运算符产生相同的结果,因为两者都执行不区分大小写的匹配。这里i
表示不敏感。让我们在下面的示例中看看它们。
使用 -Contains 和 -iContains 运算符:
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
echo "Results using '-Contains':"
$list -Contains "Wednesday"
$list -Contains "wednesday"
$list -Contains "WeDnesDay"
echo "Results using '-iContains':"
$list -iContains "Wednesday"
$list -iContains "wednesday"
$list -iContains "WeDnesDay"
输出 :
Results using '-Contains':
True
True
True
Results using '-iContains':
True
True
True
看,我们对所有匹配项都得到了 True
。现在,考虑一个我们必须执行区分大小写的匹配的用例。在这种情况下,我们将使用 -cContains
运算符;请参见以下示例。
使用 -cContains 运算符:
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
$list -cContains "Wednesday"
$list -cContains "wednesday"
$list -cContains "WeDnesDay"
输出 :
True
False
False
我们在第一场比赛中只得到了 True
,因为 -cContains
对 "wednesday"
和 `"WeDnesDay 返回了 False
“ 搜索字符串。
在 PowerShell 中,为 -Contains
运算符添加 i
和 c
前缀(如 -iContains
和 -cContains
) 执行不区分大小写和区分大小写的匹配。请记住,默认情况下,-Contains
生成与 -iContains
运算符相同的输出。
让我们通过添加一个 if-else
块来显示有意义的消息,使此代码对读者更加友好。请参阅以下示例。
使用 -cContains 运算符:
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
$searchString = "Wednesday"
$result = $list -cContains $searchString
if($result){
Write-Host "'$searchString' is found."
}else{
Write-Host "'$searchString' is not found."
}
输出 :
'Wednesday' is found.
上面的输出似乎更适合读者并且易于理解。为此,我们将布尔值存储在 $result
变量中,该变量由 $list -cContains $searchString
表达式返回。
之后,我们使用 if
语句并将 $result
变量传递给它。如果$result
的值为True
,则执行if
块;否则,else
块将是。最后,我们在 if
和 else
块中使用 Write-Host
cmdlet 在 PowerShell 控制台上显示自定义且有意义的消息。
使用 Contains()
方法
使用 Contains()
方法评估列表是否包含 PowerShell 中的指定字符串。
使用 Contains() 方法:
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
$searchString = "Wednesday"
$result = $list.Contains($searchString)
if($result){
Write-Host "'$searchString' is found."
}else{
Write-Host "'$searchString' is not found."
}
输出 :
'Wednesday' is found.
此代码示例与上一个代码片段类似,但这次我们使用了 Contains()
方法。 Contains()
方法将 $searchString
作为参数,并返回一个布尔值,我们将其存储在 $result
变量中。布尔值可以是 True
或 False
,其中 True
表示找到匹配,而 False
表示未找到匹配。
为了更容易理解,我们将 $result
传递给 if
语句,只有当 $result
的值满足时才会执行该语句。为真;否则,将执行 else
块中的 Write-Host
cmdlet。
Contains()
方法默认执行区分大小写的匹配,并且我们没有直接方法使用 Contains()
方法执行不区分大小写的匹配。但是,我们可以使用 iContains
(在上一节中学习过)、-ilike
和 -like
运算符。我们将在本文后面学习 -like
和 -ilike
。
使用 -like
运算符
使用 -like
运算符检查列表是否包含 PowerShell 中的字符串。
使用类运算符:
$fruitList = @("orange", "melon", "cherry", "banana")
$searchPattern = "*an*"
$matchedStrings = $fruitList | Where-Object {$_ -like $searchPattern}
if ($matchedStrings.Count -gt 0) {
Write-Host "List contains '$matchedStrings'."
} else {
Write-Host "List does not contain '$matchedStrings'."
}
输出 :
List contains 'orange banana'.
首先,我们创建了一个水果列表并将其存储在 $fruitList
变量中。接下来,我们使用 "*an*"
模式初始化 $searchPattern
变量。此模式意味着匹配的字符串必须完全包含 an
,而 *
通配符则显示任意数量的字符。我们在 an
前后使用了 *
,因此匹配的字符串在 ann
个字符/代码>。请记住,an
之前和之后的字符数可能相同,也可能不同。
之后,我们将 $fruitList
通过管道传输到 Where-Object
cmdlet,该 cmdlet 迭代 $fruitList
的每个项目并使用以下命令执行部分匹配-like
运算符。 -like
运算符通过执行部分匹配来查找某个项目的多次出现,而通配符模式则用于灵活的搜索。这里,$_
代表 PowerShell 中的当前项目。
我们将匹配的字符串存储在 $matchedStrings
变量中,并将其传递给 if
语句。在 if
语句中,我们使用了 $matchedStrings
变量的 Count
属性,并检查它是否大于 0
使用 -gt
运算符(大于运算符)。如果是,我们从 if
块执行 Write-Host
cmdlet 以显示一条消息,说明找到了匹配项;否则,将执行 else
块中的 Write-Host
,表示未找到匹配项。
值得注意的是,-like
运算符默认不区分大小写,这与 -ilike
运算符相同。使用 -like
和 -ilike
运算符,我们得到模式 "*an*"
、"*AN* 的相同输出”
和 “*aN*”
。另一方面,-clike
进行区分大小写的匹配。请参阅以下示例以了解其中的差异。
使用 -like、-ilike、-clike 运算符:
$fruitList = @("orange", "melon", "cherry", "banana")
echo "Match using -like Operator:"
$fruitList | Where-Object {$_ -like "*an*"}
$fruitList | Where-Object {$_ -like "*AN*"}
$fruitList | Where-Object {$_ -like "*aN*"}
echo "Match using -ilike Operator:"
$fruitList | Where-Object {$_ -ilike "*an*"}
$fruitList | Where-Object {$_ -ilike "*AN*"}
$fruitList | Where-Object {$_ -ilike "*aN*"}
echo "Match using -clike Operator:"
$fruitList | Where-Object {$_ -clike "*an*"}
$fruitList | Where-Object {$_ -clike "*AN*"}
$fruitList | Where-Object {$_ -clike "*aN*"}
输出 :
Match using -like Operator:
orange
banana
orange
banana
orange
banana
Match using -ilike Operator:
orange
banana
orange
banana
orange
banana
Match using -clike Operator:
orange
banana
对于 -like
和 -ilike
运算符,我们三次获得两个匹配值(orange
和 banana
),因为值与 "*an*"
、"*AN*"
和 "*aN*"
模式匹配。另一方面,-clike
一次返回了两个匹配的值(orange
和 banana
),因为找到了 " 的匹配项。仅 *an*"
模式。
使用 -in
运算符
使用-in
运算符检查列表是否包含PowerShell中指定的字符串。
使用 -in 运算符:
$fruitList = @("orange", "melon", "cherry", "banana")
$searchString = "orange"
$match = $searchString -in $fruitList
if ($match) {
Write-Host "List contains '$searchString'."
} else {
Write-Host "List does not contain '$searchString'."
}
输出 :
List contains 'orange'.
这次我们使用 -in
运算符来确定 $searchString
是否在 $fruitList
中。如果是,我们将得到True
;否则,False
。我们将此返回的布尔值存储在 $match
中,以将其传递给 if
语句。现在,如果 $match
变量的值为 True
,则将执行 if
语句。
请注意,-in
运算符执行不区分大小写的匹配,这意味着如果我们使用 "Orange"
初始化 $searchString
,它仍然会执行if
块并返回预期的输出。请参阅以下示例。
使用 -in 运算符:
$fruitList = @("orange", "melon", "cherry", "banana")
$searchString = "Orange"
$match = $searchString -in $fruitList
if ($match) {
Write-Host "List contains '$searchString'."
} else {
Write-Host "List does not contain '$searchString'."
}
输出 :
List contains 'Orange'.
-in
运算符不适用于 "*an*"
等搜索模式。
使用 IndexOf() 方法
使用 IndexOf()
方法确定列表是否包含 PowerShell 中的字符串。
使用 IndexOf() 方法:
$list = @("Mehvish", "Tania", "Divya")
$searchString = "Divya"
$index = $list.IndexOf($searchString)
if ($index -ge 0) {
Write-Host "'$searchString' found in list at index $index."
} else {
Write-Host "List does not contain '$searchString'."
}
输出 :
'Divya' found in list at index 2.
对于此示例,我们创建了一个包含名字的列表,并将该列表存储在 $list
变量中。接下来,我们定义了 $searchString
变量并将其值设置为 "Divya"
。之后,我们将 IndexOf()
方法与 $list
链接起来。此方法将 $searchString
作为参数,如果找到字符串匹配则返回索引。
我们将返回的索引存储在 $index
变量中。然后,$index
被传递到 if
语句,我们在其中使用 -ge
运算符来确定 的值是否为$index
变量大于或等于 0
。如果是,我们使用Write-Host
打印匹配的字符串以及相应的索引;否则,else
块将被执行。
请注意,IndexOf()
执行区分大小写的匹配,这意味着如果我们指定 "divya"
而不是 块将被执行。否则,else
块中的 Write-Host
将在 PowerShell 控制台上显示指定的消息。
使用 ForEach()
方法
使用 ForEach()
方法检查列表是否包含 PowerShell 中的字符串。
使用 ForEach() 方法:
$list = @("apple", "banana", "cherry")
$searchString = "apple"
$matched = $false
$list.ForEach({
if ($_ -eq $searchString) {
$matched=$true
}
})
if ($matched) {
Write-Host "List contains '$searchString'."
} else {
Write-Host "List does not contain '$searchString'."
}
输出 :
List contains 'apple'.
设置 $list
和 $searchString
变量的值后,我们使用 $false
初始化 $matched
价值;该值将用于表示是否找到匹配项。接下来,我们使用 ForEach()
方法来迭代所有列表项。最后,我们对每个列表项使用 if
语句和 -eq
运算符来检查当前列表项(由 $_
表示)是否等于$searchString
。如果是,则将 $matched
变量的值设置为 $true
。
最后,我们将 $matched
传递给 ForEach()
方法外部的另一个 if
语句。如果$matched
的值为True
,则执行if
块;否则,else
块将是。
此代码将为 "apple"
和 "AppLe"
搜索字符串生成相同的输出,因为 ForEach 中的
方法执行不区分大小写的比较。-eq
运算符()
使用使用 List()
创建列表的所有方法
将上述所有方法与使用 PowerShell 中的 List()
方法创建的列表结合使用。
对使用 List() 创建的列表使用所有方法:
#Create a list
$list = New-Object System.Collections.Generic.List[string]
#Add list items
$list.Add("apple")
$list.Add("orange")
$list.Add("grapes")
#returns True if string found
$list -Contains "grapes"
#returns True if string found
$list.Contains("grapes")
#returns matched string if found
$list | Where-Object {$_ -like "gra*"}
#returns True if string found
"grapes" -in $list
#returns index of the string,
#if it is greater than or equal to 0,
#then string is found
$list.IndexOf("grapes")
#returns matched string if found
$list.Where({$_ -eq "grapes"})
#prints "string found", if it is found
ForEach($item in $list){
if ($item -eq "grapes") {
Write-Host "string found."
}
}
#returns True if string found
[string]$list -match "grapes"
输出 :
True
True
grapes
True
2
grapes
string found.
True
使用使用 ArrayList()
创建列表的所有方法
将上述所有方法与使用 PowerShell 中的 ArrayList()
方法创建的列表结合使用。
对使用 ArrayList() 创建的列表使用所有方法:
#Create a list
$list = New-Object System.Collections.ArrayList
#Add list items
$list.Add("apple") #returns 0 index
$list.Add("orange") #returns 1 index
$list.Add("grapes") #returns 2 index
#returns True if string found
$list -Contains "grapes"
#returns True if string found
$list.Contains("grapes")
#returns matched string if found
$list | Where-Object {$_ -like "gra*"}
#returns True if string found
"grapes" -in $list
#returns index of the string,
#if it is greater than or equal to 0,
#then string is found
$list.IndexOf("grapes")
#returns matched string if found
$list.Where({$_ -eq "grapes"})
#prints "string found", if it is found
ForEach($item in $list){
if ($item -eq "grapes") {
Write-Host "string found."
}
}
#returns True if string found
[string]$list -match "grapes"
输出 :
0
1
2
True
True
grapes
True
2
grapes
string found.
True
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag