的-contains
运营商是不是字符串操作,但收集容器操作:
'a','b','c' -contains 'b' # correct use of -contains against collection
从about_Comparison_Operators
help topic:
Type Operator Description
Containment -contains Returns true when reference value contained in a collection
-notcontains Returns true when reference value not contained in a collection
-in Returns true when test value contained in a collection
-notin Returns true when test value not contained in a collection
通常你会使用-like
串操作者在PowerShell,该支持Windows式通配符匹配(*
用于任何数目的任何字符,?
为任何字符的正好一个,[abcdef]
对于一个字符集的一个):
'abc' -like '*b*' # $true
'abc' -like 'a*' # $true
另一种替代方法是-match
操作:
'abc' -match 'b' # $true
'abc' -match '^a' # $true
逐字串匹配,你会想逃避任何输入模式,因为-match
是一个正则表达式运算符:
'abc.e' -match [regex]::Escape('c.e')
一种替代方法是使用String.Contains()
方法:
'abc'.Contains('b') # $true
随着的是,不像的powershell字符串运算,它是大小写敏感的警告。
String.IndexOf()
是另一种选择,这一个可以让你覆盖默认的情况下,灵敏度:
'ABC'.IndexOf('b', [System.StringComparison]::InvariantCultureIgnoreCase) -ge 0
IndexOf()
返回-1
如果没有找到子串,所以任何非负的返回值可以被解释为找到了子字符串。
你”再loo ''匹配'Windows''或'-like'* Windows *'',包含的内容仅用于数组。 – ConnorLSW
的'-match'运营商,据我了解,是正则表达式,其中包含通配符的较小的子集工作,这样也能发挥作用。但是如果我想恰当地使用'contains'运算符,我该如何做?我的问题是不是我*如何做到这一点?*而是*为什么会发生这种从来没有为我工作,我在做什么错这个操作在这里吗?*有100办法做的事。我可以从.NET基类库中调用'“字符串值”.Contains()'。 –
[PowerShell和的-contains运算符(https://stackoverflow.com/questions/18877580/powershell-and-the-contains-operator) –