Книга: Practical Common Lisp
Character Comparisons
Character Comparisons
The main thing you can do with characters, other than putting them into strings (which I'll get to later in this chapter), is to compare them with other characters. Since characters aren't numbers, you can't use the numeric comparison functions, such as <
and >
. Instead, two sets of functions provide character-specific analogs to the numeric comparators; one set is case-sensitive and the other case-insensitive.
The case-sensitive analog to the numeric =
is the function CHAR=
. Like =
, CHAR=
can take any number of arguments and returns true only if they're all the same character. The case- insensitive version is CHAR-EQUAL
.
The rest of the character comparators follow this same naming scheme: the case-sensitive comparators are named by prepending the analogous numeric comparator with CHAR
; the case-insensitive versions spell out the comparator name, separated from the CHAR
with a hyphen. Note, however, that <=
and >=
are "spelled out" with the logical equivalents NOT-GREATERP
and NOT-LESSP
rather than the more verbose LESSP-OR-EQUALP
and GREATERP-OR-EQUALP
. Like their numeric counterparts, all these functions can take one or more arguments. Table 10-1 summarizes the relation between the numeric and character comparison functions.
Table 10-1. Character Comparison Functions
Numeric Analog | Case-Sensitive | Case-Insensitive |
= |
CHAR= |
CHAR-EQUAL |
/= |
CHAR/= |
CHAR-NOT-EQUAL |
< |
CHAR< |
CHAR-LESSP |
> |
CHAR> |
CHAR-GREATERP |
<= |
CHAR<= |
CHAR-NOT-GREATERP |
>= |
CHAR>= |
CHAR-NOT-LESSP |
among other things, testing whether given character is alphabetic or a digit character, testing the case of a character, obtaining a corresponding character in a different case, and translating between numeric values representing character codes and actual character objects. Again, for complete details, see your favorite Common Lisp reference.