Skip to main content

graphic-char-p

graphic-char-p Function

Syntax:

graphic-char-p char → generalized-boolean

Arguments and Values:

char—a character .

generalized-boolean—a generalized boolean.

Description:

Returns true if character is a graphic character ; otherwise, returns false.

Examples:

(graphic-char-p #\G) → true 
(graphic-char-p #\#) → true
(graphic-char-p #\Space) → true
(graphic-char-p #\Newline) → false

Exceptional Situations:

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

See Also:

read, Section 2.1 (Character Syntax), Section 13.1.10 (Documentation of Implementation-Defined Scripts)

Expanded Reference: graphic-char-p

Basic graphic character tests

graphic-char-p returns true if the character is a graphic (visible or space) character. Letters, digits, punctuation, and the space character are all graphic.

(graphic-char-p #\A)
=> T
(graphic-char-p #\a)
=> T
(graphic-char-p #\5)
=> T
(graphic-char-p #\#)
=> T
(graphic-char-p #\Space)
=> T

Non-graphic characters

Newline, tab, and other control characters are not graphic.

(graphic-char-p #\Newline)
=> NIL
(graphic-char-p #\Tab)
=> NIL
(graphic-char-p #\Return)
=> NIL

Space is graphic, Newline is not

An important distinction: the space character is considered graphic, but the newline character is not.

(graphic-char-p #\Space)
=> T
(graphic-char-p #\Newline)
=> NIL

Filtering graphic characters from a string

You can use graphic-char-p to strip non-graphic characters (like newlines and tabs) from text.

(remove-if-not #'graphic-char-p
(format nil "Hello~%World~%"))
=> "HelloWorld"

(remove-if-not #'graphic-char-p
(format nil "Line 1~CLine 2" #\Tab))
=> "Line 1Line 2"

Relationship with other character predicates

All alphanumeric characters are graphic. All standard characters except Newline are graphic.

;; Every alphanumeric character is graphic
(every #'graphic-char-p
(remove-if-not #'alphanumericp
"abc123!@#"))
=> T

(standard-char-p #\Newline)
=> T
(graphic-char-p #\Newline)
=> NIL