display | more...
Prev Up Next

It is often convenient to associate ports with strings. Thus, the procedure open-input-string associates a port with a given string. Reader procedures on this port will read off the string:

(define i (open-input-string "hello world"))

(read-char i)
=> #\h

(read i)
=> ello

(read i)
=> world

The procedure open-output-string creates an output port that will eventually be used to create a string:

(define o (open-output-string))

(write 'hello o)
(write-char #\, o)
(display " " o)
(display "world" o)

You can now use the procedure get-output-string to get the accumulated string in the string port o:

(get-output-string o)
=> "hello, world"

String ports need not be explicitly closed.

Prev Up Next