vulhig
Last Updated: June 08, 2016
·
483
· superboum
Avatar1

PowerShell: Contains does not always mean the same thing

-contains

It applies on an array only, to check if something is in it :

# On an array
$fruits = 'banana', 'apple','orange'
$fruits -contains 'banana' # Will be true
$fruits -contains 'bana' # Will be false, not an exact element

# On a string
$msg = 'I am eating a banana'
$msg -contains 'banana' # Will be false, it only match exact elements (in an array)

.contains()

On a string, it checks if a substring is present in the main string.
On an array, it checks if the elements exists, the same way as -contains

# On a string
$msg = 'I am eating a banana'
$msg.contains('banana') # Will be true, banana is a substring of $msg
$msg.contains('bana') # Will be true, bana is a substring of $msg

# On an array
$fruits = 'banana', 'apple','orange'
$fruits.contains('banana') # Will be true, banana is an element of $fruits
$fruits.contains('bana') # Will be false, bana is not an element of $fruits
Say Thanks
Respond