Next: , Previous: Regular Expressions, Up: Strings


6.9 Modification of Strings

— procedure: string-replace string char1 char2
— procedure: substring-replace string start end char1 char2
— procedure: string-replace! string char1 char2
— procedure: substring-replace! string start end char1 char2

These procedures replace all occurrences of char1 with char2 in the original string (substring). string-replace and substring-replace return a newly allocated string containing the result. string-replace! and substring-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"
— procedure: string-fill! string char

Stores char in every element of string and returns an unspecified value.

— procedure: substring-fill! string start end char

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                                         "  ******  "
— procedure: substring-move-left! string1 start1 end1 string2 start2
— procedure: substring-move-right! string1 start1 end1 string2 start2

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"
— procedure: reverse-string string
— procedure: reverse-substring string start end
— procedure: reverse-string! string
— procedure: reverse-substring! string start end

Reverses the order of the characters in the given string or substring. reverse-string and reverse-substring return newly allocated strings; reverse-string! and reverse-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"