gpt4 book ai didi

powershell - 比较字符串和数组时,PowerShell Substiture -eq是否包含-eq

转载 作者:行者123 更新时间:2023-12-03 00:28:27 24 4
gpt4 key购买 nike

今天,在PowerShell中将字符串与数组进行比较时,我错误地使用了-eq而不是-contains,但仍然得到了预期的结果。

示例代码:

$arr = "Fred","Bob"
if($arr -eq "Bob")
{
Return $true
}
Else
{
Return $false
}

即,上述内容的输出为$ true,比较字符串“Fred”的结果为$ true,但与“Steve”进行比较的结果为$ false。如果使用-contains,也会发生同样的情况。

看来PowerShell已“帮助了我”,并将我的-eq变成了-contains。这个假设正确吗?

第二,PowerShell是否有其他原因(据我所知)无法做到这一点呢?

干杯,

皮特

附言同样,如果我使用-contains将一个字符串与另一个字符串进行比较,则如果两个字符串完全匹配,它将仅返回$ true。

最佳答案

看这段代码。

$arr = "Fred", "Bob"
$arr -eq "Bob" # Bob

the docs的相关部分是

When the input to an operator is a scalar value, comparison operators return a Boolean value. When the input is a collection of values, the comparison operators return any matching values.



在这种情况下,输入是值的集合。但是,如果我们扭转该较早的表达式,以使输入(左侧)是标量值“Bob”,我们将得到完全不同的结果。
'Bob' -eq $arr   # False

关于powershell - 比较字符串和数组时,PowerShell Substiture -eq是否包含-eq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45351116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com