DataBasic Reference > Statements and Intrinsic Functions > C > COMPARE Function

Comment on this topic

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)

To

Reality

Version

Topic

Submitted by

Company

Location

Email address

Comment

 

 

COMPARE Function

Compares two strings and returns a numeric value indicating the result.

Syntax

COMPARE(string1,string2{,alignment})

Syntax Elements

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.

Return Value

The COMPARE function returns the following values:

-1 string1 < string2

0 string1 = string2

1 string1 > string2

Operation

The strings are compared as follows:

  1. If right alignment is specified and and the strings are of different lengths, the longer string is considered the greater.
  2. Otherwise, the comparison proceeds character by character from left to right. Characters are compared by ASCII value (see Decimal, Hexadecimal, and ASCII Table).

    Note: If the case-insensitive option is specified or data case insensitive mode is selected (see Case Sensitivity), case is ignored when comparing characters.
  3. With left alignment specified, if the strings are of different lengths and each character in the shorter string matches the character at the same position in the other, the longer string is considered the greater.

Examples

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.

RealityV15.0Comment on this topic