Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - $_.contains versus -contains

Reply
 
Old 07-07-2007   #1 (permalink)
Marco Shaw


 
 

$_.contains versus -contains

$_.contains versus -contains...

Should these behave differently?

175# $test=@"
>> test 123
>> "@
>>

176# $test -contains 123
False
177# $test -contains "123"
False
178# $test|foreach-object{$_.contains("123")}
True
179# $test -contains "test 123"
True

(I seem to remember that I think it was also different behaviour between
$_.replace and -replace.)

Marco

My System SpecsSystem Spec
Old 07-07-2007   #2 (permalink)
Kiron


 
 

Re: $_.contains versus -contains

I think $_.contains('x') is similar to $_ -match 'x' or $_ -like "*x*"

$_ -contains 'x' checks 'x' against a collection of elements in $_ to match.

"test 123".contains('1')

"test 123" -match 1

"test 123", "456", 768 -contains 456


--
Kiron

My System SpecsSystem Spec
Old 07-07-2007   #3 (permalink)
Keith Hill


 
 

Re: $_.contains versus -contains

"Marco Shaw" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
news:Or3eJcKwHHA.3508@TK2MSFTNGP03.phx.gbl...
> $_.contains versus -contains...
>
> Should these behave differently?
>
> 175# $test=@"
> >> test 123
> >> "@
> >>

> 176# $test -contains 123
> False
> 177# $test -contains "123"
> False
> 178# $test|foreach-object{$_.contains("123")}
> True
> 179# $test -contains "test 123"
> True


Yes they are pretty different and it is reasonable that they behave
differently. $_.contains is really just System.String.Contains which was
designed by the .NET Framework BCL team. Those folks have a much more
strict definition of how String.Contains works which you would expect from a
lower level framework type. -contains however is a PowerShell operator and
PowerShell goes to great lengths to just make the operator work as you would
expect it to in the face of different data types and dimensionality (scalar
vs array).

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
64 bit versus 32 bit question. Vista General
C# versus Java .NET General
Vista Versus XP General Discussion
8.5 versus 2008 versus 9 Live Messenger
64 bit versus 32 bit Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46