Isa*_*aac 5 replace gimp script-fu str-replace
我有一个Gimp插件重命名文件,我需要一个替换功能.不幸的是,Gimp使用的TinyScheme没有字符串的替换函数.我搜索了很多,但找不到一个真正的字符串替换.回答如下......
这是我创建的实现.如果有更好的解决方案,请随时告诉我.
(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 "")
)
)
Run Code Online (Sandbox Code Playgroud)