Converting VBScript's StrComp Function

Definition: Returns a value indicating the result of a string comparison.

StrComp

Can we tell you whether two strings or identical? We can, provided you tell us one thing: do you want a case-sensitive comparison or a case-insensitive comparison?

Let’s try a case-insensitive comparison for starters. In the following set of commands we assign values to variables $a and $b. We then use the System.String class and the Compare() method to compare the two strings. Note that we pass the Compare() method three parameters: the two strings to be compared, and the value $True, which indicates that we want to do a case-insensitive comparison:

$a = "dog"
$b = "DOG"
$c = [String]::Compare($a,$b,$True)

When you run this command and then echo back the value of $c you should get the following:

0

Note. A 0 means that the strings are identical. Any other value means that the strings are not identical.

Of course, these strings are identical because we did a case-insensitive comparison. Suppose we use this command, which does a case -sensitive comparison:

$c = [String]::Compare($a,$b,$False)

This time you get back -1, meaning that the strings are not identical (because the letter cases are not the same).

Return to the VBScript to Windows PowerShell home page