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.
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
(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)))
Comments top
No comments for str. Log in to add a comment.