char-upcase, char-downcase
char-upcase, char-downcase Function
Syntax:
char-upcase character → corresponding-character
char-downcase character → corresponding-character
Arguments and Values:
character, corresponding-character—a character .
Description:
If character is a lowercase character , char-upcase returns the corresponding uppercase character . Otherwise, char-upcase just returns the given character.
If character is an uppercase character , char-downcase returns the corresponding lowercase character . Otherwise, char-downcase just returns the given character.
The result only ever differs from character in its code attribute; all implementation-defined attributes are preserved.
Examples:
(char-upcase #\a) → #\A
(char-upcase #\A) → #\A
(char-downcase #\a) → #\a
(char-downcase #\A) → #\a
(char-upcase #\9) → #\9
(char-downcase #\9) → #\9
(char-upcase #\@) → #\@
(char-downcase #\@) → #\@
;; Note that this next example might run for a very long time in
;; some implementations if CHAR-CODE-LIMIT happens to be very large
;; for that implementation.
(dotimes (code char-code-limit)
(let ((char (code-char code)))
(when char
(unless (cond ((upper-case-p char) (char= (char-upcase (char-downcase char)) char)) ((lower-case-p char) (char= (char-downcase (char-upcase char)) char))
(t (and (char= (char-upcase (char-downcase char)) char)
(char= (char-downcase (char-upcase char)) char))))
(return char)))))
→ NIL
Exceptional Situations:
Should signal an error of type type-error if character is not a character .
See Also:
upper-case-p, alpha-char-p, Section 13.1.4.3 (Characters With Case), Section 13.1.10 (Documen tation of Implementation-Defined Scripts)
Notes:
If the corresponding-char is different than character, then both the character and the corresponding char have case.
Since char-equal ignores the case of the characters it compares, the corresponding-character is always the same as character under char-equal.
Expanded Reference: char-upcase, char-downcase
Converting letter case
char-upcase converts a lowercase letter to uppercase. char-downcase converts an uppercase letter to lowercase.
(char-upcase #\a)
=> #\A
(char-upcase #\z)
=> #\Z
(char-downcase #\A)
=> #\a
(char-downcase #\Z)
=> #\z
Characters without case are returned unchanged
Non-alphabetic characters and characters that are already the target case are returned as-is.
(char-upcase #\A)
=> #\A
(char-downcase #\a)
=> #\a
(char-upcase #\5)
=> #\5
(char-downcase #\5)
=> #\5
(char-upcase #\@)
=> #\@
(char-downcase #\Space)
=> #\Space
Case conversion round-trip
Converting up then down (or down then up) always returns to the original character for characters with case.
(char-downcase (char-upcase #\a))
=> #\a
(char-upcase (char-downcase #\Z))
=> #\Z
(char-downcase (char-upcase #\5))
=> #\5
Building a simple string case converter
char-upcase and char-downcase operate on individual characters. You can use them with map to convert entire strings.
(map 'string #'char-upcase "Hello, World!")
=> "HELLO, WORLD!"
(map 'string #'char-downcase "Hello, World!")
=> "hello, world!"
Swapping case of each character
(defun swap-case (char)
(cond ((upper-case-p char) (char-downcase char))
((lower-case-p char) (char-upcase char))
(t char)))
(map 'string #'swap-case "Hello, World!")
=> "hELLO, wORLD!"