Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare the contents of two string objects in PowerShell

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?

like image 406
hax0r_n_code Avatar asked Sep 12 '13 18:09

hax0r_n_code


People also ask

How do you compare two things in PowerShell?

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.

How do you compare two strings of elements?

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.

How do you check if a string is equal to another string in PowerShell?

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.


1 Answers

You want to do $arrayOfString[0].Title -eq $myPbiject.item(0).Title

-match is for regex matching ( the second argument is a regex )

like image 89
manojlds Avatar answered Sep 22 '22 02:09

manojlds