Skip to main content

make-string-input-stream

make-string-input-stream Function

Syntax:

make-string-input-stream string &optional start end → string-stream

Arguments and Values:

string—a string.

start, endbounding index designators of string. The defaults for start and end are 0 and nil, respectively.

string-stream—an input string stream.

Description:

Returns an input string stream. This stream will supply, in order, the characters in the substring of string bounded by start and end. After the last character has been supplied, the string stream will then be at end of file.

Examples:

(let ((string-stream (make-string-input-stream "1 one "))) 
(list (read string-stream nil nil)
(read string-stream nil nil)
(read string-stream nil nil)))
(1 ONE NIL)
(read (make-string-input-stream "prefixtargetsuffix" 6 12)) → TARGET

See Also:

with-input-from-string

Expanded Reference: make-string-input-stream

Basic Usage

make-string-input-stream creates an input stream that reads characters from a given string.

(let ((s (make-string-input-stream "hello")))
(list (read-char s)
(read-char s)
(read-char s)))
=> (#\h #\e #\l)

Reading Lisp Objects from a String

The stream can be used with read to parse Lisp objects from a string.

(let ((s (make-string-input-stream "1 one ")))
(list (read s nil nil)
(read s nil nil)
(read s nil nil)))
=> (1 ONE NIL)

Using Start and End Parameters

The optional start and end arguments restrict which portion of the string is read.

(read (make-string-input-stream "prefixtargetsuffix" 6 12))
=> TARGET

Detecting End of File

When all characters have been consumed, the stream reaches end of file.

(let ((s (make-string-input-stream "ab")))
(list (read-char s nil :eof)
(read-char s nil :eof)
(read-char s nil :eof)))
=> (#\a #\b :EOF)

Reading Lines from a String Stream

read-line works with string input streams to read line-delimited text.

(let ((s (make-string-input-stream (format nil "line1~%line2~%line3"))))
(list (read-line s)
(read-line s)
(read-line s nil :done)))
=> ("line1" "line2" "line3")