Skip to main content

string

string System Class

Class Precedence List:

string, vector, array, sequence, t

Description:

A string is a specialized vector whose elements are of type character or a subtype of type character. When used as a type specifier for object creation, string means (vector character).

Compound Type Specifier Kind:

Abbreviating.

Compound Type Specifier Syntax:

(string [size])

Compound Type Specifier Arguments:

size—a non-negative fixnum, or the symbol *.

Compound Type Specifier Description:

This denotes the union of all types (array c (size)) for all subtypes c of character; that is, the set of strings of size size.

See Also:

Section 16.1 (String Concepts), Section 2.4.5 (Double-Quote), Section 22.1.3.4 (Printing Strings)

Expanded Reference: string (System Class)

Strings are specialized vectors of characters

A string is a one-dimensional array (vector) whose elements are characters. String literals are written with double quotes.

(type-of "hello")
=> (SIMPLE-ARRAY CHARACTER (5))

(typep "hello" 'string)
=> T

(typep "hello" 'vector)
=> T

Strings are sequences and arrays

Because strings are vectors, all sequence and array operations work on them.

(length "Common Lisp")
=> 11

(reverse "Common Lisp")
=> "psiL nommoC"

(elt "abcdef" 3)
=> #\d

Using the string type specifier with a size

The compound type specifier (string size) denotes strings of a particular length.

(typep "hello" '(string 5))
=> T

(typep "hello" '(string 3))
=> NIL

(typep "hello" '(string *))
=> T

Creating strings with make-array

Strings can be created explicitly using make-array with a character element type.

(make-array 5 :element-type 'character :initial-element #\x)
=> "xxxxx"

(let ((s (make-array 5 :element-type 'character :initial-contents "hello")))
(typep s 'string))
=> T

Strings with fill pointers

Strings created with fill pointers are still strings, but they are not simple strings.

(let ((s (make-array 10 :element-type 'character
:fill-pointer 5
:initial-element #\a)))
(list (typep s 'string)
(length s)
(array-total-size s)))
=> (T 5 10)