In PowerShell I have an array of string objects, and I have an object that contains string objects. In Java you can do a .equals(aObject)
to test if the string values match, whereas doing a ==
test if the two objects refer to the same location in memory.
How do I run an equivalent .equals(aObject)
in powershell?
I'm doing this:
$arrayOfStrings[0].Title -matches $myObject.item(0).Title
These both have the exact same string values, but I get a return value of false. Any suggestions?
Compare-Object command in PowerShell is used to compare two objects. Objects can be a variable content, two files, strings, etc. This cmdlet uses few syntaxes to show the difference between objects which is called side indicators. => - Difference in destination object.
In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same. The input string has to be a char array of C-style string.
To check to see if one object is equal to another object in PowerShell is done using the eq operator. The eq operator compares simple objects of many types such as strings, boolean values, integers and so on. When used, the eq operator will either return a boolean True or False value depending on the result.
You want to do $arrayOfString[0].Title -eq $myPbiject.item(0).Title
-match
is for regex matching ( the second argument is a regex )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With