Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, October 24, 2018 6:33 AM
As per test, I know that:
1. Contains() only can be used for the string
2. -contains only can be used for array
But I cannot find any official document to explain that, could somebody let me know that is there any official document to explain the difference between them?
All replies (8)
Wednesday, October 24, 2018 6:55 AM | 2 votes
There is no official document that states that. You must look up each separately.
help about_Comparison_Operators
The "Contains() method is defined on each type. Look up the type to get the definition.
$x = 1,2,3
$x.Contains(2)
Most collections have a "contains" method.
$f = [float]1.1,[float]1.2,[float]9.4
$f.Contains([float]1.2)
[collections.arraylist]$l=9,8,7,6
$l.Contains(8)
See. No strings attached.
\(ツ)_/
Wednesday, October 24, 2018 12:20 PM
Here's the String.Contains .net method. Note that it is case sensitive. There's also .replace() and .split() and many others on the left side.
/en-us/dotnet/api/system.string.contains?view=netframework-4.7.2
You can use -match instead of .contains() and it's not case sensitive.
PS C:\ 'hithere' -match 'Hi'
True
Wednesday, October 24, 2018 6:52 PM | 1 vote
Now about:
1) Contains() is a method. Every type (i.e. class) contains its own implementation. For example:
"abc".Contains('b') # TRUE
("a","b", "c").Contains('b') # TRUE
("aa","bb", "cc").Contains('b') # FALSE
2) -contains is an operator. It's part of the Powershell language.
"abc" -contains "b" # FALSE
("a", "b", "c") -contains "b" # TRUE
("aa", "bb", "cc") -contains "b" # FALSE
Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Thursday, October 25, 2018 1:53 AM
Hi Rich, I know that. Just want to know if there is any official doc to explain the usage of -contains.
Thursday, October 25, 2018 5:10 AM | 1 vote
Hi Rich, I know that. Just want to know if there is any official doc to explain the usage of -contains.
As noted above. The Net framework documents for each collection type describes the "Contains()" method and the help file "about_comparison_operators" are the official documents describing these two items. There is no official document that comapres them. They are two things. One is the Net AAPI and the other is a PowerShell syntax element.
The syntax element automatically calls the correct Framework method. Please read the help file.
Your assumption that one only works with strings is completely incorrect. All collection types can have a "contains" method. See the Net documentation for a discussion of how classes in this object system are designed to work.
For background on object programming and system see:
https://en.wikipedia.org/wiki/Object-oriented_programming
For a less technical discussion see:
https://medium.freecodecamp.org/object-oriented-programming-concepts-21bb035f7260
\(ツ)_/
Thursday, October 25, 2018 5:37 AM | 1 vote
I can try to explain this to you in a different way. Perhaps that will help you to understand why your question does not have the kind of answer you seek.
In the Net Framework that is a class (Type) that is named System.Text.RegularExpressions.Regex. The type accelerator for this is exposed in PowerShell as [regex]. This also has a syntax operator in PowerShell -matches,
The type is documented in the Net Framework documentation here: /en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.7.2
The other elements are documented in the help for PowerShell syntax operators and the exisitence of the type accelerator is documented in the release notes and in the PowerShell tutorials available on the Microsoft site.
There is no formal comparison of these items because it is unnecessary. Knowing these comes as an outgrowth of learning PowerShell. For those who have not learned PowerShell but whoa are trying to guess at how it works most of the important lessons are never learned. Most blogs are designed to add to the basic training in PowerShell and do not address the required elements for fully understanding the blog post. Many other blogs are just simple examples with little background and may also contain incorrect information.
There are many excellent books for beginning users of PowerShell. Anyone wanting to use PowerShell as more than a replacement for the old CMD shell script should do one or more of these books to gain the basics of PS. There is also one video that can get you started down the correct path.
Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
There are also more video tutorials for workflow and DSC as well as other aspects of PowerShell.
\(ツ)_/
Thursday, October 25, 2018 7:09 AM
Thanks for share.
Just do it.
Thursday, October 25, 2018 12:37 PM
Hi Rich, I know that. Just want to know if there is any official doc to explain the usage of -contains.