Product Site

5.1.7.19. Comparison Methods

Returns .true or .false, the result of performing the specified comparison operation. The receiver object and the argument are the terms compared. Both must be string objects. If argument is not a string object, it is converted to its string representation for the comparison. The one exception is when argument is .nil for the ==, \==, =, \=, ><, and <> operators. A string object will never compare equal to .nil, even when the string matches the string value of .nil ("The NIL object"). As a result, == will always return .false when compared to .nil and \== will always return .true. All of the relational comparisons (for example, <, >, <=, etc.) will always return .false when compared to .nil.

The comparison operators you can use in a message are:
=

.true if the terms are equal (for example, numerically or when padded). .false if argument is .nil.
\=, ><, <>

.true if the terms are not equal (inverse of =). .true if argument is .nil.
>

Greater than. .false if argument is .nil.
<

Less than. .false if argument is .nil.
>=

Greater than or equal to. .false if argument is .nil.
\<

Not less than. .false if argument is .nil.
<=

Less than or equal to. .false if argument is .nil.
\>

Not greater than. .false if argument is .nil.
Example 5.55. String class — comparison methods
Say 5=5        -->     1          /* equal            */

Say 42\=41     -->     1          /* All of these are */
Say 42><41     -->     1          /* "not equal"      */
Say 42<>41     -->     1

Say 13>12      -->     1          /* Variations of    */
Say 12<13      -->     1          /* less than and    */
Say 13>=12     -->     1          /* greater than     */
Say 12\<13     -->     0
Say 12<=13     -->     1
Say 12\>13     -->     1

All strict comparison operations have one of the characters doubled that define the operator. The == and \== operators check whether two strings match exactly. The two strings must be identical (character by character) and of the same length to be considered strictly equal.

The strict comparison operators such as >> or << carry out a simple character-by-character comparison. There is no padding of either of the strings being compared. The comparison of the two strings is from left to right. If one string is shorter than and a leading substring of another, then it is smaller than (less than) the other. The strict comparison operators do not attempt to perform a numeric comparison on the two operands.

For all the other comparison operators, if both terms are numeric, the String class does a numeric comparison (ignoring, for example, leading zeros—see Section 10.4, “Numeric Comparisons”). Otherwise, it treats both terms as character strings, ignoring leading and trailing whitespace characters and padding the shorter string on the right with blanks.

Character comparison and strict comparison operations are both case-sensitive, and for both the exact collating order can depend on the character set. In an ASCII environment, the digits are lower than the alphabetic characters, and lowercase alphabetic characters are higher than uppercase alphabetic characters.

The strict comparison operators you can use in a message are:
==

.true if terms are strictly equal (identical)
\==

.true if the terms are NOT strictly equal (inverse of ==)
>>

Strictly greater than
<<

Strictly less than
>>=

Strictly greater than or equal to
\<<

Strictly NOT less than
<<=

Strictly less than or equal to
\>>

Strictly NOT greater than
Example 5.56. String class — comparison methods
Say "space"=="space"     -->     1         /* Strictly equal     */

Say "space"\==" space"   -->     1         /* Strictly not equal */

Say "space">>" space"    -->     1         /* Variations of      */
Say " space"<<"space"    -->     1         /* strictly greater   */
Say "space">>=" space"   -->     1         /* than and less than */
Say "space"\<<" space"   -->     1
Say " space"<<="space"   -->     1
Say " space"\>>"space"   -->     1