如何使用另一个字符串创建一个替换子字符串的函数,该子字符串的开始和结束索引

时间:2017-10-16 12:52:36

标签: clojure

我有一个字符串"你好"我想用另一个字符串替换两个索引之间的字符,比如" Foo"。 E.g。

(defn new-replace [orig-str start-index end-index new-string] ...)

(= "Foollo" (new-replace "Hello" 0 2 "Foo")) => true
(= "Foolo" (new-replace "Hello" 0 3 "Foo")) => true

有什么建议吗?干杯

2 个答案:

答案 0 :(得分:0)

这是一种方式:

(defn new-replace [orig-str start-index end-index new-string]
   (str (apply str (take start-index orig-str))
        new-string
        (apply str (drop end-index orig-str))))

答案 1 :(得分:0)

Stringbuffer已经提供了替换功能:

(defn new-replace [orig-str start-index end-index new-string]                                                                                                                                                      
  (str (.replace (StringBuffer. orig-str) start-index end-index new-string)))  
相关问题