Characters and Strings

5.1 Characters

#\a  #\A  #\Space  #\Newline  #\Tab  #\λ
(char-code #\A)           ; 65
(code-char 955)           ; #\GREEK_SMALL_LETTER_LAMDA
(char-upcase #\a)         ; #\A
(alpha-char-p #\a) (digit-char-p #\7) (upper-case-p #\A) (alphanumericp #\_)
(digit-char-p #\7)        ; 7 (returns the digit's value)
(char= #\a #\a) (char-equal #\a #\A) (char< #\a #\b)

5.2 Strings

Mutability: literal strings ("..." in source) are constants; don't modify them. Use (copy-seq "...") or make-string for mutable strings. Adjustable strings with fill pointers support vector-push-extend.

For richer string utilities, use str ((str:split "," "a,b,c"), (str:join ", " list), (str:replace-all "a" "b" s)), cl-ppcre (regex), and split-sequence.

(defparameter *s* "Hello, Lisp World")
(length *s*)                          ; 17
(char *s* 0)                          ; #\H
(aref *s* 1)                          ; #\e (strings are vectors)
(subseq *s* 7 11)                     ; "Lisp"
(string-upcase *s*) (string-downcase *s*) (string-capitalize "hello world")   ; "Hello World"
(string-trim " " "  padded  ")        ; "padded"
(string-left-trim "0" "007")          ; "7"
(concatenate 'string "Hello" ", " "World")
(search "Lisp" *s*)                   ; 7
(position #\, *s*)                    ; 5
(count #\l *s*)                       ; 3
(reverse "abc")                       ; "cba"
(string= "abc" "abc")                 ; T (case-sensitive)
(string-equal "ABC" "abc")            ; T (case-insensitive)
(string< "apple" "banana")            ; 0 (mismatch index, i.e. true)
(string "symbol-name")                ; coerce a string designator
(format nil "~a-~a" "a" "b")          ; "a-b": the general string builder
(with-output-to-string (s)            ; efficient string building
  (dotimes (i 3) (format s "~d," i))) ; "0,1,2,"
(coerce "abc" 'list)                  ; (#\a #\b #\c)
(coerce '(#\a #\b) 'string)           ; "ab"
(map 'string #
…