Skip to main content

char-name

char-name Function

Syntax:

char-name character → name

Arguments and Values:

character—a character .

name—a string or nil.

Description:

Returns a string that is the name of the character, or nil if the character has no name.

All non-graphic characters are required to have names unless they have some implementation-defined attribute which is not null. Whether or not other characters have names is implementation dependent.

The standard characters ⟨Newline⟩ and ⟨Space⟩ have the respective names "Newline" and "Space". The semi-standard characters ⟨Tab⟩, ⟨Page⟩, ⟨Rubout⟩, ⟨Linefeed⟩, ⟨Return⟩, and ⟨Backspace⟩ (if they are supported by the implementation) have the respective names "Tab", "Page", "Rubout", "Linefeed", "Return", and "Backspace" (in the indicated case, even though name lookup by “#\” and by the function name-char is not case sensitive).

Examples:

(char-name #\ )"Space" 
(char-name #\Space)"Space"
(char-name #\Page)"Page"
(char-name #\a)
→ NIL
<i><sup>or</sup>→</i> "LOWERCASE-a"
<i><sup>or</sup>→</i> "Small-A"
<i><sup>or</sup>→</i> "LA01"
(char-name #\A)
→ NIL
<i><sup>or</sup>→</i> "UPPERCASE-A"
<i><sup>or</sup>→</i> "Capital-A"
<i><sup>or</sup>→</i> "LA02"
;; Even though its CHAR-NAME can vary, #\A prints as #\A
(prin1-to-string (read-from-string (format nil "#\\~A" (or (char-name #\A) "A"))))"#\\A"


Exceptional Situations:

Should signal an error of type type-error if character is not a character .

See Also:

name-char, Section 22.1.3.2 (Printing Characters)

Notes:

Non-graphic characters having names are written by the Lisp printer as “#\” followed by the their name; see Section 22.1.3.2 (Printing Characters).

Expanded Reference: char-name

Named non-graphic characters

char-name returns the name of a character as a string, or NIL if the character has no name. Non-graphic characters are required to have names.

(char-name #\Space)
=> "Space"
(char-name #\Newline)
=> "Newline"
(char-name #\Tab)
=> "Tab"
(char-name #\Return)
=> "Return"

Graphic characters may or may not have names

Whether graphic characters (letters, digits, punctuation) have names is implementation-dependent. Most implementations return NIL for them.

(char-name #\a)
;; => NIL or implementation-defined name
(char-name #\A)
;; => NIL or implementation-defined name
(char-name #\5)
;; => NIL or implementation-defined name

The Space character has a name

The space character is the only standard graphic character that has a required name.

(char-name #\ )
=> "Space"
(char-name #\Space)
=> "Space"

Round-tripping with name-char

char-name and name-char are inverses for characters that have names.

(name-char (char-name #\Space))
=> #\Space
(name-char (char-name #\Newline))
=> #\Newline

;; For characters with names, the round trip always works
(let ((name (char-name #\Space)))
(when name
(char= (name-char name) #\Space)))
=> T

Listing names of characters in a string

You can inspect which characters in a string have names.

(mapcar #'char-name (coerce (format nil "A B~%C") 'list))
;; => (NIL "Space" NIL "Newline" NIL) or implementation-dependent