Documentation Comments
Use this form to comment on this topic. You can also provide any general observations about the Online Documentation, or request that additional information be added in a future release.
Reality V15.0 ()
COMPARE Function (DataBasic) (m618703+compare_f.htm)
Compares two strings and returns a numeric value indicating the result.
COMPARE(string1,string2{,alignment})
string1, string2The string expressions to be compared.
alignmentOptional. Specifies whether the strings are to be compared as if left- or right-aligned (see Operation). If specified, must evaluate to a string containing "L" (left) or "R" (right), optionally followed by "S" (case-sensitive) or "I" (case-insensitive). If not specified, the default is left-aligned. If the specified value is none of these, a run time error occurs.
The COMPARE function returns the following values:
-1 string1 < string2
0 string1 = string2
1 string1 > string2
The strings are compared as follows:
Otherwise, the comparison proceeds character by character from left to right. Characters are compared by ASCII value (see Decimal, Hexadecimal, and ASCII Table).
A="XYZ11" B="XYZ100" PRINT COMPARE(A,B,'L') PRINT COMPARE(A,B,'R')
In this case, the first PRINT statement displays 1, because the ASCII code of the final character in A ('1' - 49) is greater than that of the corresponding character in B ('0' - 48). The second PRINT statement, however, displays -1, because right-alignment is selected and string A is longer than string B.
A="XYZ101" B="XYZ100" PRINT COMPARE(A,B,'L') PRINT COMPARE(A,B,'R')
In this case, both strings are the same length and both PRINT statements display 1 because the ASCII code of the final character in A ('1' - 49) is greater than that of the corresponding character in B ('0' - 48).
A="XYZ10" B="XYZ100" PRINT COMPARE(A,B,'L') PRINT COMPARE(A,B,'R')
In the final example, both PRINT statements display -1 (B > A). In the first case (with left alignment selected), all the characters in A match the corresponding ones in B, but B is longer. In the second case, B is simply longer than A and, because right alignment is selected, no character comparisons are performed.