Construction of Strings - MIT/GNU Scheme 9.2
Description: MIT/GNU Scheme 9.2

Next: Selecting String Components, Previous: Strings, Up: Strings

### 6.1 Construction of Strings

— procedure: make-string k [char]

> Returns a newly allocated string of length k. If you specify char, all elements of the string are initialized to char, otherwise the contents of the string are unspecified. Char must satisfy the predicate `char-ascii?`.

>               (make-string 10 #\x)              =>  "xxxxxxxxxx"

— procedure: string char ...

> Returns a newly allocated string consisting of the specified characters. The arguments must all satisfy `char-ascii?`.

>               (string #\a)                                =>  "a"
>               (string #\a #\b #\c)                        =>  "abc"
>               (string #\a #\space #\b #\space #\c)        =>  "a b c"
>               (string)                                    =>  ""

— procedure: list->string char-list

> Char-list must be a list of ISO-8859-1 characters. `list->string` returns a newly allocated string formed from the elements of char-list. This is equivalent to `(apply string` char-list`)`. The inverse of this operation is `string->list`.

>               (list->string '(#\a #\b))           =>  "ab"
>               (string->list "Hello")              =>  (#\H #\e #\l #\l #\o)

— procedure: string-copy string

> Returns a newly allocated copy of string.

> Note regarding variable-length strings: the maximum length of the result depends only on the length of string, not its maximum length. If you wish to copy a string and preserve its maximum length, do the following:

>               (define (string-copy-preserving-max-length string)
>                 (let ((length))
>                   (dynamic-wind
>                    (lambda ()
>                      (set! length (string-length string))
>                      (set-string-length! string
>                                          (string-maximum-length string)))
>                    (lambda ()
>                      (string-copy string))
>                    (lambda ()
>                      (set-string-length! string length)))))