write-string, write-line
write-string, write-line Function
Syntax:
write-string string &optional output-stream &key start end → string
write-line string &optional output-stream &key start end → string
Arguments and Values:
string—a string.
output-stream – an output stream designator . The default is standard output.
start, end—bounding index designators of string. The defaults for start and end are 0 and nil, respectively.
Description:
write-string writes the characters of the subsequence of string bounded by start and end to output-stream. write-line does the same thing, but then outputs a newline afterwards.
Examples:
(prog1 (write-string "books" nil :end 4) (write-string "worms"))
▷ bookworms
→ "books"
(progn (write-char #\\*)
(write-line "test12" \*standard-output\* :end 5)
(write-line "\*test2")
(write-char #\\*)
nil)
▷ \*test1
▷ \*test2
▷ \*
→ NIL
Affected By:
*standard-output*, *terminal-io*.
See Also:
read-line, write-char
Notes:
write-line and write-string return string, not the substring bounded by start and end.
(write-string string)
≡ (dotimes (i (length string)
(write-char (char string i)))
(write-line string)
≡ (prog1 (write-string string) (terpri))
Expanded Reference: write-string, write-line
Basic write-string
write-string writes characters from a string to an output stream. It returns the original string (not the substring).
(with-output-to-string (s)
(write-string "hello world" s))
=> "hello world"
write-string with :start and :end
The :start and :end keyword arguments select a substring to write.
(with-output-to-string (s)
(write-string "bookworms" s :end 4))
=> "book"
Basic write-line
write-line works like write-string but appends a newline after the string.
(with-output-to-string (s)
(write-line "first" s)
(write-line "second" s))
=> "first
second
"
Combining write-string and write-line
(with-output-to-string (s)
(write-string "Name: " s)
(write-line "Alice" s)
(write-string "Age: " s)
(write-string "30" s))
=> "Name: Alice
Age: 30"
Return Value
Both functions return the full original string, not the written substring.
(with-output-to-string (s)
(let ((result (write-string "bookworms" s :end 4)))
(write-char #\Space s)
(write-string result s)))
=> "book bookworms"