LISP - String Comparison
Lisp - String Comparison
Section titled “Lisp - String Comparison”Standard numeric operators like < and > do not work for comparing strings in Common Lisp. Instead, the language provides dedicated sets of functions for both case-sensitive and case-insensitive string comparison.
These functions are more powerful than simple equality checks, as they can compare substrings and operate on sequences of strings.
| Case-Sensitive | Case-Insensitive | Description |
|---|---|---|
string= | string-equal | Returns T if the strings are identical, NIL otherwise. |
string/= | string-not-equal | Returns the index of the first mismatch if strings differ, NIL otherwise. |
string< | string-lessp | Returns mismatch index if string1 is lexicographically less than string2, else NIL. |
string> | string-greaterp | Returns mismatch index if string1 is lexicographically greater than string2, else NIL. |
string<= | string-not-greaterp | Returns mismatch index if string1 is not greater than string2, else NIL. |
string>= | string-not-lessp | Returns mismatch index if string1 is not less than string2, else NIL. |
Key Concept: Many comparison functions return a “generalized boolean”. NIL is always false. Any other value, including a number like the mismatch index 0, is considered true.
Example: Case-Sensitive Comparison
Section titled “Example: Case-Sensitive Comparison”Case-sensitive functions perform a character-by-character comparison based on their character codes. Uppercase letters have different codes than lowercase letters.
main.lisp
Section titled “main.lisp”(format t "'test' = 'Test' -> ~a~%" (string= "test" "Test"))
;; "test" is lexicographically greater than "Test" because 't' > 'T'.;; The function returns the index of the first character mismatch.(format t "'test' > 'Test' -> ~a~%" (string> "test" "Test"))
;; "apple" is less than "banana". Mismatch is at index 1 ('p' vs 'a').(format t "'apple' < 'banana' -> ~a~%" (string< "apple" "banana"))Output
Section titled “Output”When you execute the code, it returns the following result:
'test' = 'Test' -> NIL'test' > 'Test' -> 0'apple' < 'banana' -> 1Example: Case-Insensitive Comparison
Section titled “Example: Case-Insensitive Comparison”Case-insensitive functions are generally preferred for user input or text that should be compared semantically, ignoring capitalization.
main.lisp
Section titled “main.lisp”(format t "'test' equal 'Test' -> ~a~%" (string-equal "test" "Test"))
;; When ignoring case, "test" is not greater than "Test".(format t "'test' greaterp 'Test' -> ~a~%" (string-greaterp "test" "Test"))
;; 'apple' is still less than 'banana' when ignoring case.(format t "'apple' lessp 'banana' -> ~a~%" (string-lessp "apple" "banana"))Output
Section titled “Output”The results now ignore case:
'test' equal 'Test' -> T'test' greaterp 'Test' -> NIL'apple' lessp 'banana' -> 1Comparing Substrings with Start/End Indices
Section titled “Comparing Substrings with Start/End Indices”All string comparison functions accept optional :start and :end keyword arguments for each string, allowing you to compare specific portions of strings.
main.lisp
Section titled “main.lisp”;; Compare the substring "world" from the first string with the second string.(let ((s1 "hello world") (s2 "world")) (format t "Comparing substring: ~a~%" (string-equal s2 s1 :start2 6)))Output
Section titled “Output”The comparison succeeds because we are only looking at the relevant part of s1:
Comparing substring: T