/
strCompare

strCompare

Description

Compares two strings alphabetically.

If the first string is "before" the second string, a value less than 0 is returned. If the second string is "before" the first string, a value greater than 0 is returned. If the two strings are equal, 0 is returned.

By default, the string comparison is case-sensitive (e.g. `A` is less than `a`). It compares ASCII character values, with capital letters (A-Z) being before lower case letters (a-z). This can be overridden with the -noCase flag.

This command is useful for sorting lists of strings. If you just want to determine if two strings are identical, use the equality operator `==` on the two strings.

Functional area

String

Command syntax

Syntax

strCompare "string1" "string2" [-noCase]

Arguments

NameTypeRequiredComments
string1stringyesFirst string to be compared.
string2stringyesSecond string to be compared.

Flags

NameFlag argumentsArgument typeExclusive toComments
noCase0Specifies that the search be case insensitive. The default is case sensitive.

Return value

integer

See Description section above for explanation of the return value.

Examples

// Demonstrate string comparison.
string $str1 = "Bob";
string $str2 = "Stan";
string $str3 = "bob";
int $val;
 
// Compare the first two strings.
$val = `strCompare $str1 $str2`;
print $val; 
// Should be less than 0
 
// Compare the second and third strings.
$val = `strCompare $str2 $str3`;
print $val; 
// Should be less than 0
 
// Compare the first and the third.
$val = `strCompare $str1 $str3`;print $val; 
// Should be less than 0
 
// Do a case insensitive search of the first and third
$val = `strCompare $str1 $str3 -noCase`;
print $val; 
// Should be 0

Additional information

Related commands