Skip to main content

make-string

make-string Function

Syntax:

make-string size &key initial-element element-type → string

Arguments and Values:

size—a valid array dimension.

initial-element—a character . The default is implementation-dependent.

element-type—a type specifier . The default is character.

string—a simple string.

Description:

make-string returns a simple string of length size whose elements have been initialized to initial-element.

The element-type names the type of the elements of the string; a string is constructed of the most specialized type that can accommodate elements of the given type.

Examples:

(make-string 10 :initial-element #\5)"5555555555" 
(length (make-string 10))10

Affected By:

The implementation.

Expanded Reference: make-string

Creating a string of a given size

make-string creates a simple string of the specified length. You should provide :initial-element to ensure the contents are well-defined.

(make-string 5 :initial-element #\a)
=> "aaaaa"

(make-string 0)
=> ""

The result is always a simple string

The string returned by make-string is always a simple string -- it has no fill pointer and is not displaced or adjustable.

(simple-string-p (make-string 10 :initial-element #\x))
=> T

Checking the length of the result

The resulting string has exactly the requested length.

(length (make-string 8 :initial-element #\*))
=> 8

(length (make-string 0))
=> 0

Specifying element-type

The :element-type keyword controls the character subtype. Using base-char creates a base-string.

(let ((s (make-string 3 :initial-element #\z :element-type 'base-char)))
(list s (typep s 'base-string)))
=> ("zzz" T)

(let ((s (make-string 3 :initial-element #\z :element-type 'character)))
(list s (typep s 'string)))
=> ("zzz" T)

Practical use: building a separator line

(let ((line (make-string 40 :initial-element #\-)))
(concatenate 'string "+" line "+"))
=> "+----------------------------------------+"