On Fri, Oct 15, 2010 at 16:25, Paul <paul_bow...@yahoo.com> wrote:
> Hi all,
>
> I need to perform string replacement a number of times and currently
> achieve this using:
>
> (.replace (.replace (.replace (.replace <string> <old> <new>...
>
> Is there a more succinct and 'clojurish' way to do this?
>

Instead of
  (.replace (.replace (.replace "ABC" "A" "a") "B" "b") "C" "c")

you can write:
  (-> "ABC" (.replace "A" "a") (.replace "B" "b") (.replace "C" "c"))

http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/->

If the list of replacements is quite long, or not known at coding
time, you could do something like this:

(reduce
    (fn [orig [old new]] (.replace orig old new))
    "ABC"
    [["A" "a"] ["B" "b"] ["C" "c"]])

http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/reduce

// Ben

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to