将输出写入字符串

WITH-OUTPUT-TO-STRING 可用于创建字符串输出流,并在结尾返回结果字符串。

(with-output-to-string (str)
  (write-line "Foobar!" str)
  (write-string "Barfoo!" str))
;=> "Foobar!
;   Barfoo!"

同样可以使用 MAKE-STRING-OUTPUT-STREAMGET-OUTPUT-STREAM-STRING 手动完成。

(let ((str (make-string-output-stream)))
  (write-line "Foobar!" str)
  (write-string "Barfoo!" str)
  (get-output-stream-string str))