Common Lisp the Language, 2nd Edition


next up previous contents index
Next: String Comparison Up: Strings Previous: Strings

18.1. String Access

The following functions access a single character element of a string.


[Function]
char string index
schar simple-string index

The given index must be a non-negative integer less than the length of string, which must be a string. The character at position index of the string is returned as a character object. (This character will necessarily satisfy the predicate string-char-p.)

change_begin
X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate string-char-p.
change_end

As with all sequences in Common Lisp, indexing is zero-origin. For example:

(char "Floob-Boober-Bab-Boober-Bubs" 0) => #\F 
(char "Floob-Boober-Bab-Boober-Bubs" 1) => #\l

See aref and elt. In effect,

(char s j) == (aref (the string s) j)

setf may be used with char to destructively replace a character within a string.

For char, the string may be any string; for schar, it must be a simple string. In some implementations of Common Lisp, the function schar may be faster than char when it is applicable.


AI.Repository@cs.cmu.edu