chickadee » srfi-152 » string-replace

(string-replace string₁ string₂ start₁ end₁ [start₂ end₂]) → stringprocedure

Returns

(string-append (substring string₁ 0 start₁)
               (substring string₂ start₂ end₂)
               (substring string₁ end₁ (string-length string₁)))

That is, the segment of characters in string₁ from start₁ to end₁ is replaced by the segment of characters in string₂ from start₂ to end₂. If start₁ = end₁, this simply splices the characters drawn from string₂ into string₁ at that position.

Examples:

(string-replace "The TCL programmer endured daily ridicule."
                 "another miserable perl drone" 4 7 8 22)
    ⇒ "The miserable perl programmer endured daily ridicule."

(string-replace "It's easy to code it up in Scheme." "lots of fun" 5 9)
    ⇒ "It's lots of fun to code it up in Scheme."

(define (string-insert s i t) (string-replace s t i i))

(string-insert "It's easy to code it up in Scheme." 5 "really ")
    ⇒ "It's really easy to code it up in Scheme."

(define (string-set s i c) (string-replace s (string c) i (+ i 1)))

(string-set "String-ref runs in O(n) time." 21 #\1)
    ⇒ "String-ref runs in O(1) time."