Next: , Previous: String Selection, Up: Strings


21.4.6 String Modification

These procedures are for modifying strings in-place. This means that the result of the operation is not a new string; instead, the original string's memory representation is modified.

— Scheme Procedure: string-set! str k chr
— C Function: scm_string_set_x (str, k, chr)

Store CHR in element K of STRING and returns an unspecified value. K must be a valid index of STR.

— Scheme Procedure: string-fill! str chr [start [end]]
— C Function: scm_string_fill_x (str, chr, start, end) |2 |2 |0

Store chr in every element of the given str. The return value is unspecified.

— Scheme Procedure: substring-fill! str start end fill
— C Function: scm_substring_fill_x (str, start, end, fill)

Change every character in str between start and end to fill char.

          (define y "abcdefg")
          (substring-fill! y 1 3 #\r)
          y
          ⇒ "arrdefg"
— Scheme Procedure: substring-move! str1 start1 end1 str2 start2
— Scheme Procedure: substring-move-right! str1 start1 end1 str2 start2
— Scheme Procedure: substring-move-left! str1 start1 end1 str2 start2
— C Function: scm_substring_move_x (str1, start1, end1, str2, start2)

Copy the substring of str1 bounded by start1 and end1 into str2 beginning at position end2. substring-move-right! begins copying from the rightmost character and moves left, and substring-move-left! copies from the leftmost character moving right.

It is useful to have two functions that copy in different directions so that substrings can be copied back and forth within a single string. If you wish to copy text from the left-hand side of a string to the right-hand side of the same string, and the source and destination overlap, you must be careful to copy the rightmost characters of the text first, to avoid clobbering your data. Hence, when str1 and str2 are the same string, you should use substring-move-right! when moving text from left to right, and substring-move-left! otherwise. If str1 and ‘str2’ are different strings, it does not matter which function you use.