如何在PowerShell中使用“-In”运算符?字符串是否构成另一个字符串?

时间:2018-04-05 10:20:55

标签: powershell

如果一个字符串是否包含另一个字符串,我想用它来填充。

例如:

if("234" -In "1234 56)
{
#do something
}

似乎有效。

But if I have variables how can I do this?
For example: 
$a = "234"
$b = "123456"

4 个答案:

答案 0 :(得分:2)

运算符 -In 搜索比较数组/集合中是否存在值。像这样:

$Arr = 1,2,3,4,5

5 -in $Arr
True

6 -in $Arr
False

-In 运算符在Powershell 3及更高版本中可用。

答案 1 :(得分:0)

您正在寻找运营商-match:

"abcdefghijklmnopqrstuvwxyz" -match "def" ##returns $True

查看about_Comparison_Operators了解详情。

答案 2 :(得分:0)

变量的一个例子可能是这样的。

$string1 = "234"    
$string2 = "123456"

    foreach($s in $string2)
    {
        if($s.Contains($string1))
        {
           Write-Host "It does contain"
        }
        else{
            Write-Host "does not contain"
        }
    }

答案 3 :(得分:0)

正如其他人所指出的,-in不是您问题的解决方案。但是,将来如果您确实需要有关PowerShell运算符的信息,那么可以在内置帮助中找到一个好的起点:

Get-Help about_Operators

本主题概述,并指出更具体的主题。在这种情况下,您可以从-in获得更多帮助:

Get-Help about_Comparison_Operators