1.3.0 permalink Arrow_down_16x16
  • (str)
  • (str x)
  • (str x & ys)
With no args, returns the empty string. With one arg x, returns
x.toString(). (str nil) returns the empty string. With more than
one arg, returns the concatenation of the str values of the args.

2 Examples top

  • user=> "some string"
    "some string"
    
    user=> (str)
    ""
    user=> (str nil)
    ""
    user=> (str 1)
    "1"
    user=> (str 1 2 3)
    "123"
    user=> (str 1 'symbol :keyword)
    "1symbol:keyword"
    
    ;; A very common usage of str is to apply it to an existing collection:
    user=> (apply str [1 2 3])
    "123"
    
    ;; compare it with:
    user=> (str [1 2 3])
    "[1 2 3]"
    
    
  • ;; Destructuring with a string, getting just a few characters from it
    user=> (let [[first-char second-char] "abcde"] 
             (prn 'first= first-char) 
             (prn 'second= second-char))
    first= \a
    second= \b
    nil
    
    ;; More destructuring with a string
    user=> (let [[first-char second-char & rest-of-chars] "abcde"] 
             (prn 'first= first-char) 
             (prn 'second= second-char) 
             (prn 'rest= rest-of-chars))
    first= \a
    second= \b
    rest= (\c \d \e)
    nil
    
    ;; Destructuring, getting the first character of a string
    ;; and then a reference to the entire string
    user=> (let [[first-char :as all-the-string] "abcde"] 
             (prn 'first= first-char) 
             (prn 'all= all-the-string))
    first= \a
    all= "abcde"
    nil
Log in to add / edit an example.

See Also top

  • 0
    clojure.core/pr

    Prints the object(s) to the output stream that is the current value

  • 0
    clojure.core/prn

    Same as pr followed by (newline). Observes *flush-on-newline*

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:487 top

(defn str
  "With no args, returns the empty string. With one arg x, returns
  x.toString().  (str nil) returns the empty string. With more than
  one arg, returns the concatenation of the str values of the args."
  {:tag String
   :added "1.0"
   :static true}
  (^String [] "")
  (^String [^Object x]
   (if (nil? x) "" (. x (toString))))
  (^String [x & ys]
     ((fn [^StringBuilder sb more]
          (if more
            (recur (. sb  (append (str (first more)))) (next more))
            (str sb)))
      (new StringBuilder (str x)) ys)))
Vars in clojure.core/str:
Used in 0 other vars

Comments top

No comments for str. Log in to add a comment.