1.3.0 permalink Arrow_down_16x16

replace

clojure.string

  • (replace s match replacement)
Replaces all instance of match with replacement in s.

match/replacement can be:

string / string
char / char
pattern / (string or function of match).

See also replace-first.

6 Examples top

  • user=> (clojure.string/replace "The color is red" #"red" "blue")
    "The color is blue"
  • user=> (clojure.string/replace "The color is red." #"[aeiou]"  #(str %1 %1))
    "Thee cooloor iis reed."
    
  • ;; Note: When replace-first or replace have a regex pattern as their
    ;; match argument, dollar sign ($) and backslash (\) characters in
    ;; the replacement string are treated specially.
    
    ;; Example: first substring that the pattern matches is "fodder", with
    ;; (o+) matching "o" and (\S+) matching "dder".  Replacement string
    ;; says to replace the entire match "fodder" with $2, the string
    ;; matched by the second parenthesized group, "dder", followed by $1,
    ;; "o".
    user=> (str/replace "fabulous fodder foo food" #"f(o+)(\S+)" "$2$1")
    "fabulous ddero oo doo"
    
    ;; To avoid this special treatment of $ and \, you must escape them with
    ;; \.  Because it is in a Clojure string, to get one \ we must escape
    ;; *that* with its own \.
    user=> (str/replace "fabulous fodder foo food" #"f(o+)(\S+)" "\\$2\\$1")
    "fabulous $2$1 $2$1 $2$1"
    
    ;; To ensure the replacement is treated literally, call
    ;; java.util.regex.Matcher/quoteReplacement on it.  A shorter name
    ;; like re-qr can be handy.
    user=> (import '(java.util.regex Matcher))
    java.util.regex.Matcher
    
    user=> (defn re-qr [replacement]
             (Matcher/quoteReplacement replacement))
    #'user/re-qr
    
    user=> (str/replace "fabulous fodder foo food" #"f(o+)(\S+)" (re-qr "$2$1"))
    "fabulous $2$1 $2$1 $2$1"
    
  • ;; replaces all a's with 1 and all b's with 2
    user=>(clojure.string/replace "a b a" #"a|b" {"a" "1" "b" "2"})
    "1 2 1"
  • ;; Note: See clojure.core/subs for discussion of behavior of substrings
    ;; holding onto references of the original strings, which can
    ;; significantly affect your memory usage in some cases.
  • ;; To title case
    (clojure.string/replace "hello world" #"\b." #(.toUpperCase %1))
    "Hello World"
    ;; Note that a vector is passed to your replacement function
    ;; when pattern contains capturing groups (see re-groups)
    (clojure.string/replace "hello world" #"\b(.)" #(.toUpperCase (%1 1)))
    "Hello World"
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/string.clj:63 top

(defn ^String replace
  "Replaces all instance of match with replacement in s.

   match/replacement can be:

   string / string
   char / char
   pattern / (string or function of match).

   See also replace-first."
  {:added "1.2"}
  [^CharSequence s match replacement]
  (let [s (.toString s)]
    (cond 
     (instance? Character match) (.replace s ^Character match ^Character replacement)
     (instance? CharSequence match) (.replace s ^CharSequence match ^CharSequence replacement)
     (instance? Pattern match) (if (instance? CharSequence replacement)
                                 (.replaceAll (re-matcher ^Pattern match s)
                                              (.toString ^CharSequence replacement))
                                 (replace-by s match replacement))
     :else (throw (IllegalArgumentException. (str "Invalid match arg: " match))))))
Vars in clojure.string/replace:
Used in 0 other vars

Comments top

1 comment(s) for replace.

How can i replace "." with #"\s"

Log in to add a comment.