5

我有一个重命名文件的 Gimp 插件,我需要一个替换功能。不幸的是,Gimp 使用的 TinyScheme 没有字符串的替换功能。我搜索了很多,但找不到一个真正的字符串替换。回复关注...

4

1 回答 1

7

这是我创建的实现。如果有更好的解决方案,请随时告诉我。

(define (string-replace strIn strReplace strReplaceWith)
    (let*
        (
            (curIndex 0)
            (replaceLen (string-length strReplace))
            (replaceWithLen (string-length strReplaceWith))
            (inLen (string-length strIn))
            (result strIn)
        )
        ;loop through the main string searching for the substring
        (while (<= (+ curIndex replaceLen) inLen)
            ;check to see if the substring is a match
            (if (substring-equal? strReplace result curIndex (+ curIndex replaceLen))
                (begin
                    ;create the result string
                    (set! result (string-append (substring result 0 curIndex) strReplaceWith (substring result (+ curIndex replaceLen) inLen)))
                    ;now set the current index to the end of the replacement. it will get incremented below so take 1 away so we don't miss anything
                    (set! curIndex (-(+ curIndex replaceWithLen) 1))
                    ;set new length for inLen so we can accurately grab what we need
                    (set! inLen (string-length result))
                )
            )
            (set! curIndex (+ curIndex 1))
        )
       (string-append result "")
    )
)
于 2012-07-17T15:54:26.200 回答