#PSTip CompareTo() method for comparing strings!
There are certainly many methods to compare strings in PowerShell.
Today, I will show you one of the methods that I recently came across — a less known method, maybe.
"PowerShell Magazine".CompareTo("PowerShell magazine")
The output of CompareTo() method will be zero if strings are equal and -1 or 1, otherwise.
So, what do you expect as an output for the above command? 0 or 1?
The output in this case will be 1. Well, this is because the CompareTo() method does a case sensitive search by default. But, technically, the strings in our example are not different if we discount the case of these strings. So, how do we do a case-insensitive search using CompareTo() method?
Here’s how:
[string]::Compare("PowerShell Magazine", "PowerShell magazine", $True)Share on: