Comparison Operators on String

In Common Lisp, strings are vectors, which are one-dimensional arrays of characters. Except for the double quotation character (“) and the escape character (\), every character supported by the character set can be contained between double quotes to form a string. Like Character, the string also has two sets of Comparison Operators, one is Case-sensitive and the other is Case-insensitive.

So, to check whether the two Strings are equal are not, the following are some comparison Functions that can be used.

Case-sensitive Case-insensitive Explanation
STRING= STRING-EQUAL Checks if the operands’ values are all equal; if they are, the condition is true.
STRING/= STRING-NOT-EQUAL Checks if the operands’ values are all unequal; if they are, the condition is true.
STRING< STRING-LESSP If string1 is smaller than string2, the condition is true; otherwise, the condition is false.
STRING<=      STRING-NOT-GREATERP     If any of the left operands’ values are less than or equal to the value of the following right operand, the condition is true.
STRING> STRING-GREATERP If string1 is greater than string2, the condition is true; otherwise, the condition is false.
STRING>=                STRING-NOT-LESSP                           If any left operand’s value is more than or equal to its right operand’s value, the condition is true.  

Example 1: Case Sensitive Comparison.

Lisp




(write (STRING= "gfg" "GFG"))
(terpri)
(write (STRING> "gfg" "GFG"))
(terpri)
(write (STRING< "gfg" "GFG"))


Output: When running the above code, the output is as follows.

NIL
0
NIL

Example 2: Case-insensitive Comparison.

Lisp




(write (STRING-EQUAL "gfg" "GFG"))
(terpri)
(write (STRING-GREATERP "gfg" "GFG"))
(terpri)
(write (STRING-LESSP "gfg" "GFG"))


Output: Comparison OperatorsWhen running the above code, the output is as follows.

T
NIL
NIL


LISP – Comparison Operators on Characters & Strings

The contents of a field are compared to the contents of another field or a constant using Comparison operators. In Simple words, comparator operators are used to compare whether the two or more different values are equal or not. 

Similar Reads

Comparison Operators on Character:

Characters are not supported by numerical comparison functions and operators such as < and >. Other than that, Two sets of functions in Common LISP are used. The first set is case-sensitive, whereas the second is not case-sensitive or (case-insensitive)....

Comparison Operators on String:

...