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

[玩转系统] 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 字符串比较:区分大小写与不区分大小写

查看如何在 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 字符串比较:区分大小写与不区分大小写

查看 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 脚本后查看下面屏幕截图中的输出。

[玩转系统] 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 中使用字符串比较方法

现在,让我向您展示如何使用 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 字符串比较:区分大小写与不区分大小写

阅读如何在 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 中区分大小写和不区分大小写的字符串比较之间的区别。

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

取消回复欢迎 发表评论:

关灯