[玩转系统] PowerShell 字符串比较:区分大小写与不区分大小写
作者:精品下载站 日期:2024-12-14 05:20:55 浏览:14 分类:玩电脑
PowerShell 字符串比较:区分大小写与不区分大小写
作为一名 PowerShell 开发人员,您应该了解如何在 PowerShell 中执行区分大小写和不区分大小写的字符串比较。在本教程中,我将通过完整的代码和示例指导您了解 PowerShell 中可用的各种字符串比较方法。
在 PowerShell 中,字符串比较既可以不区分大小写,也可以区分大小写。默认情况下,-eq
和 -like
等运算符不区分大小写,这意味着它们将大写字母和小写字母视为等效。对于区分大小写的比较,您可以使用 -ceq
和 -clike
等运算符。此外,.Equals()
方法允许显式区分大小写设置,为各种比较需求提供灵活性。
PowerShell 中的字符串比较
在 PowerShell 中,可以使用各种运算符和方法来执行字符串比较。默认情况下,PowerShell 不区分大小写,这意味着它将大写和小写字母视为等效。但是,在某些情况下需要区分大小写的比较。让我们详细探讨不区分大小写和区分大小写的比较。
PowerShell 中不区分大小写的字符串比较
让我向您展示如何在 PowerShell 中执行不区分大小写的字符串比较。
1.使用-eq和-ne运算符
-eq
(等于)和 -ne
(不等于)运算符用于 PowerShell 中的相等比较。默认情况下,这些运算符不区分大小写。
让我向您展示一个示例,以帮助您更好地理解它。
$string1 = "Hello"
$string2 = "hello"
if ($string1 -eq $string2) {
Write-Output "The strings are equal (case-insensitive)."
} else {
Write-Output "The strings are not equal."
}
在此示例中,输出将是:
The strings are equal (case-insensitive).
我执行了上面的 PowerShell 脚本,您可以在下面的屏幕截图中看到输出:
查看如何在 PowerShell 中将字符串转换为驼峰式大小写?
2. 使用-like和-notlike运算符
-like
和 -notlike
运算符用于与通配符进行模式匹配。默认情况下,这些运算符也不区分大小写。这是带有完整脚本的完整示例。
$string1 = "Hello"
$string2 = "h*o"
if ($string1 -like $string2) {
Write-Output "The string matches the pattern (case-insensitive)."
} else {
Write-Output "The string does not match the pattern."
}
输出将是:
The string matches the pattern (case-insensitive).
下面的屏幕截图是我执行上述 PowerShell 脚本后的输出。
查看 PowerShell If-Else 字符串比较
PowerShell 中区分大小写的字符串比较
现在,让我向您展示如何在 PowerShell 中进行区分大小写的字符串比较。
1.使用-ceq和-cne运算符
要在 PowerShell 中执行区分大小写的比较,可以使用 -ceq
(区分大小写的等于)和 -cne
(区分大小写的不等于)运算符。
这是一个例子,可以帮助您更好地理解它。
$string1 = "Hello"
$string2 = "hello"
if ($string1 -ceq $string2) {
Write-Output "The strings are equal (case-sensitive)."
} else {
Write-Output "The strings are not equal."
}
在此示例中,输出将是:
The strings are not equal.
您可以在我执行 PowerShell 脚本后查看下面屏幕截图中的输出。
2. 使用 -clike 和 -cnotlike 运算符
同样,对于区分大小写的模式匹配,您可以在 PowerShell 中使用 -clike
和 -cnotlike
运算符。这是一个例子,可以帮助您更好地理解它。
$string1 = "Hello"
$string2 = "h*o"
if ($string1 -clike $string2) {
Write-Output "The string matches the pattern (case-sensitive)."
} else {
Write-Output "The string does not match the pattern."
}
输出将是:
The string does not match the pattern.
您可以在下面的屏幕截图中看到输出:
阅读 PowerShell 比较运算符
在 PowerShell 中使用字符串比较方法
现在,让我向您展示如何使用 PowerShell 中的字符串比较方法。
1..Equals()方法
PowerShell 中的 .Equals()
方法也可用于字符串比较。它提供了指定区分大小写的选项。
这是一个例子。
$string1 = "Hello"
$string2 = "hello"
if ($string1.Equals($string2, [System.StringComparison]::OrdinalIgnoreCase)) {
Write-Output "The strings are equal (case-insensitive using Equals method)."
} else {
Write-Output "The strings are not equal."
}
if ($string1.Equals($string2, [System.StringComparison]::Ordinal)) {
Write-Output "The strings are equal (case-sensitive using Equals method)."
} else {
Write-Output "The strings are not equal."
}
在此示例中,第一次比较将输出:
The strings are equal (case-insensitive using Equals method).
第二次比较将输出:
The strings are not equal.
您可以在下面的屏幕截图中看到确切的输出:
阅读如何在 PowerShell 中从字符串中删除换行符?
2..Contains()方法
PowerShell 中的 .Contains()
方法本质上区分大小写。要执行不区分大小写的搜索,必须将两个字符串转换为相同的大小写。
让我给你举个例子。
$string1 = "Hello"
$string2 = "he"
# Case-sensitive
if ($string1.Contains($string2)) {
Write-Output "The string contains the substring (case-sensitive)."
} else {
Write-Output "The string does not contain the substring."
}
# Case-insensitive
if ($string1.ToLower().Contains($string2.ToLower())) {
Write-Output "The string contains the substring (case-insensitive)."
} else {
Write-Output "The string does not contain the substring."
}
输出将是:
The string does not contain the substring.
The string contains the substring (case-insensitive).
结论
在本教程中,我解释了如何在 PowerShell 中执行区分大小写和不区分大小写的字符串比较。我相信这些示例将帮助您了解 PowerShell 中区分大小写和不区分大小写的字符串比较之间的区别。
猜你还喜欢
- 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