26.  String Ports  

String ports are similar to file ports, except that characters are appended to a string instead of being sent to a file, or taken from a string instead of being read from a file. It is not necessary to close string ports. When an string input port has reached the end of the input string, successive read operations return end-of-file.

(open-input-string string)

Returns a new string input port initialized with string.
Examples:

(define p (open-input-string "Hello world!"))
(read-char p)                    ==>  #\H
(read p)                         ==>  ello
(read p)                         ==>  world!
(read p)                         ==>  end of file
(define p (open-input-string "(cons 'a 'b)"))
(eval (read p))                  ==>  (a . b)

(open-output-string)

Returns a new string output port.

(get-output-string string-output-port)

Returns the string currently associated with the specified string output port. As a side-effect, the string is reset to zero length.
Examples:

(define p (open-output-string))
(display '(a b c) p)
(get-output-string p)            ==>  "(a b c)"
(get-output-string p)            ==>  ""
(define (flat-size s)
  (let ((p (open-output-string)))
    (display s p)
    (string-length (get-output-string p))))


Markup created by unroff 1.0,    September 24, 1996,    net@informatik.uni-bremen.de