read-string

added
1.0

ns
clojure.core

type
function

(read-string s) (read-string opts s)

Reads one object from the string s. Optionally include reader
options, as specified in read.

Note that read-string can execute code (controlled by *read-eval*),
and as such should be used only with trusted sources.

For data structure interop use clojure.edn/read-string

                user=> (read-string "1.1")          
1.1

user=> (read-string "1.1.1 (+ 1 1)")
java.lang.RuntimeException: java.lang.NumberFormatException: Invalid number: 1.1.1 (NO_SOURCE_FILE:0)

user=> (read-string "(+ 1 1)")
(+ 1 1)

            
                user=> (eval (read-string "(+ 1 1)"))
2

user=> (read-string (prn-str (+ 1 1)))
2

            
                user=> (+ 11 (read-string "23"))
34

            
                user=> (read-string "; foo\
5")
5

user=> (read-string "#^String x")
x

user=> (read-string "(1)")
(1)

user=> (read-string "(+ 1 2) (- 3 2)")
(+ 1 2)

user=> (read-string "@a")
(clojure.core/deref a)

user=> (read-string "(+ 1 2))))))")
(+ 1 2)

user=> (read-string "::whatever-namespace-you-are-in")
:user/whatever-namespace-you-are-in
            
                ;convert a string representing a sequence,
;to the sequence that the string represents
user=> (read-string "(\\\\( \\\\x \\\\y \\\\) \\\\z)")
(\\( \\x \\y \\) \\z)

;then you can convert to the string that the string-sequence represents
user=> (apply str (read-string "(\\\\( \\\\x \\\\y \\\\) \\\\z)"))
"(xy)z"

;which is the inverse of
user=> (str (first (list (seq "(xy)z"))))
"(\\\\( \\\\x \\\\y \\\\) \\\\z)"
            
                ;; you can think of read-string as the inverse of pr-str
;; turn string into symbols
user=> (read-string "(a b foo :bar)")
(a b foo :bar)

;;turn symbols into a string
user=> (pr-str '(a b foo :bar))
"(a b foo :bar)"
            
                ;; WARNING: You SHOULD NOT use clojure.core/read-string to read data from
;; untrusted soures.  See the examples for clojure.core/read, because the same
;; issues exist for both read and read-string.
            
                ;; convert binary number provided in the form of a string to its numerical value.
user=> (read-string (str "2r" "1011"))
11