ClojureDocs

Nav

Namespaces

str

clojure.core

Available since 1.0 (source)
  • (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.
7 Examples
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
user=> (str "L" "a")
"La"

user=> (str "L" 5 "a")
"L5a"
;; sometimes when printing lazy sequences you do not get what you want.
(str (take 5 (range 10)))
;=> "clojure.lang.LazySeq@1b554e1"

;; in those cases `pr-str` to the rescue.
(pr-str (take 5 (range 10)))
;=> "(0 1 2 3 4)"
;; be careful with java.lang.Double to java.lang.String conversion
(str -128004972.0)
;=> "-1.28004972E8"
;; Using str in maps
(def user-map {:fname "Jane" :sname "Doe"})

;;getting the value of the first key 
(:fname user-map)
;=> "Jane"

(str (:fname user-map) " " (:sname user-map))
;=> "Jane Doe"
(str nil)
;; => ""

(str true)
;; => "true"

(str false)
;; => "false"
See Also

Prints the object(s) to the output stream that is the current value of *out*. Prints the object(s...

Added by JR0cket

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

Added by JR0cket

pr to a string, returning it

Added by Dimagog

prn to a string, returning it

Added by Dimagog

print to a string, returning it

Added by Dimagog

println to a string, returning it

Added by Dimagog
0 Notes
No notes for str