-contains operator vs .contains() method

I am the first person to scream RTFM or some equivalent to use search engines, but I hope the community will forgive me as I am neck deep in a project and confused about something

Would someone please give me a brief explanation as to why the -contains operator returns a different result as the .Contains method call does (as in the following example)?

PS C:\Windows\system32> $string = "abcdefg"

PS C:\Windows\system32> $sting -contains "abc"
False

PS C:\Windows\system32> $string.Contains("abc")
True
It is not immediately clear to me why these would return different results.  I know I am missing something simple and thank in advance anyone helpers.



  • Edited by RKS333 Wednesday, July 22, 2015 3:16 AM
July 22nd, 2015 3:15am

$array = "a","b","c"
$array -contains "a"

"my string".contains('my')

Now figure this one out.

PS H:\> [array]"a","b","c" -contains "a"
False
PS H:\> [array]"a","b","c" -contains "b"
True
PS H:\> [array]"a","b","c" -contains "c"
True



Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 8:31am

I don't know how to explain your examples, but contrast it with

<##> @("a","b","c") -contains "a"
True
<##> @("a","b","c") -contains "b"
True
<##> @("a","b","c") -contains "c"
True
<##>

July 22nd, 2015 9:21am

I don't know how to explain your examples, but contrast it with

<##> @("a","b","c") -contains "a"
True
<##> @("a","b","c") -contains "b"
True
<##> @("a","b","c") -contains "c"
True
<##>

Me either:-) I just found it.
Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 9:32am

From MS documentation

-Contains
      Description: Containment operator. Tells whether a collection of reference
      values includes a single test value. Always returns a Boolean value. Returns TRUE
      only when the test value exactly matches at least one of the reference values. 

It must be one of the elements in the array not a substring of a string element.

Contains Method is one of the methods of a string object that supports substring hence why you get
True when you run $string.Contains("abc")

July 22nd, 2015 11:39am

$array = "a","b","c"
$array -contains "a"

"my string".contains('my')

Now figure this one out.

PS H:\> [array]"a","b","c" -contains "a"
False
PS H:\> [array]"a","b","c" -contains "b"
True
PS H:\> [array]"a","b","c" -contains "c"
True


Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 11:44am

And if anyone can now explain Dan's example, that would be great.  :)

The first element isn't a string, so the test fails:

PS C:\> $a = [array]"a","b","c"

PS C:\> $a
a
b
c

PS C:\> $a[0]
a

PS C:\> $a[0].GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                      
-------- -------- ----                                     --------                                                                                                      
True     True     Object[]                                 System.Array                                                                                                  



PS C:\> $a[1]
b

PS C:\> $a[1].GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                      
-------- -------- ----                                     --------                                                                                                      
True     True     String                                   System.Object
July 22nd, 2015 11:45am

Of course!   Thanks, RKS333, for pointing that out.

Here is an alternative

<##> [array]("a","b","c") -contains "a"
True
<##> [array]("a","b","c") -contains "b"
True
<##> [array]("a","b","c") -contains "c"
True
<##>

Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 12:28pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics