Next: Variable-Length Strings, Previous: Regular Expressions, Up: Strings
These procedures replace all occurrences of char1 with char2 in the original string (substring).
string-replace
andsubstring-replace
return a newly allocated string containing the result.string-replace!
andsubstring-replace!
destructively modify string and return an unspecified value.(define str "a few words") => unspecified (string-replace str #\space #\-) => "a-few-words" (substring-replace str 2 9 #\space #\-) => "a few-words" str => "a few words" (string-replace! str #\space #\-) => unspecified str => "a-few-words"
Stores char in every element of string and returns an unspecified value.
Stores char in elements start (inclusive) to end (exclusive) of string and returns an unspecified value.
(define s (make-string 10 #\space)) => unspecified (substring-fill! s 2 8 #\*) => unspecified s => " ****** "
Copies the characters from start1 to end1 of string1 into string2 at the start2-th position. The characters are copied as follows (note that this is only important when string1 and string2 are
eqv?
):
substring-move-left!
- The copy starts at the left end and moves toward the right (from smaller indices to larger). Thus if string1 and string2 are the same, this procedure moves the characters toward the left inside the string.
substring-move-right!
- The copy starts at the right end and moves toward the left (from larger indices to smaller). Thus if string1 and string2 are the same, this procedure moves the characters toward the right inside the string.
The following example shows how these procedures can be used to build up a string (it would have been easier to use
string-append
):(define answer (make-string 9 #\*)) => unspecified answer => "*********" (substring-move-left! "start" 0 5 answer 0) => unspecified answer => "start****" (substring-move-left! "-end" 0 4 answer 5) => unspecified answer => "start-end"
Reverses the order of the characters in the given string or substring.
reverse-string
andreverse-substring
return newly allocated strings;reverse-string!
andreverse-substring!
modify their argument strings and return an unspecified value.(reverse-string "foo bar baz") => "zab rab oof" (reverse-substring "foo bar baz" 4 7) => "rab" (let ((foo "foo bar baz")) (reverse-string! foo) foo) => "zab rab oof" (let ((foo "foo bar baz")) (reverse-substring! foo 4 7) foo) => "foo rab baz"