Next: , Previous: , Up: Strings and Characters   [Contents][Index]


10.6 Comparison of Characters and Strings

Function: char-equal character1 character2 &optional buffer

This function returns t if the arguments represent the same character, nil otherwise. This function ignores differences in case if the value of case-fold-search is non-nil in buffer, which defaults to the current buffer.

(char-equal ?x ?x)
     ⇒ t
(let ((case-fold-search t))
  (char-equal ?x ?X))
     ⇒ t
(let ((case-fold-search nil))
  (char-equal ?x ?X))
     ⇒ nil
Function: char= character1 character2

This function returns t if the arguments represent the same character, nil otherwise. Case is significant.

(char= ?x ?x)
     ⇒ t
(char= ?x ?X)
     ⇒ nil
(let ((case-fold-search t))
  (char-equal ?x ?X))
     ⇒ nil
(let ((case-fold-search nil))
  (char-equal ?x ?X))
     ⇒ nil
Function: string= string1 string2

This function returns t if the characters of the two strings match exactly; case is significant.

(string= "abc" "abc")
     ⇒ t
(string= "abc" "ABC")
     ⇒ nil
(string= "ab" "ABC")
     ⇒ nil
Function: string-equal string1 string2

string-equal is another name for string=.

Function: string< string1 string2

This function compares two strings a character at a time. First it scans both the strings at once to find the first pair of corresponding characters that do not match. If the lesser character of those two is the character from string1, then string1 is less, and this function returns t. If the lesser character is the one from string2, then string1 is greater, and this function returns nil. If the two strings match entirely, the value is nil.

Pairs of characters are compared by their ASCII codes. Keep in mind that lower case letters have higher numeric values in the ASCII character set than their upper case counterparts; numbers and many punctuation characters have a lower numeric value than upper case letters.

(string< "abc" "abd")
     ⇒ t
(string< "abd" "abc")
     ⇒ nil
(string< "123" "abc")
     ⇒ t

When the strings have different lengths, and they match up to the length of string1, then the result is t. If they match up to the length of string2, the result is nil. A string of no characters is less than any other string.

(string< "" "abc")
     ⇒ t
(string< "ab" "abc")
     ⇒ t
(string< "abc" "")
     ⇒ nil
(string< "abc" "ab")
     ⇒ nil
(string< "" "")
     ⇒ nil
Function: string-lessp string1 string2

string-lessp is another name for string<.

See also compare-buffer-substrings in Comparing Text, for a way to compare text in buffers. The function string-match, which matches a regular expression against a string, can be used for a kind of string comparison; see Regexp Search.


Next: , Previous: , Up: Strings and Characters   [Contents][Index]