read-string

clojure.core

  • (read-string s)
Reads one object from the string s

7 Examples top

  • 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
    
  • ;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)"
  • user=> (read-string "; foo\n5")
    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
  • ;; 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.
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:3355 top

(defn read-string
  "Reads one object from the string s"
  {:added "1.0"
   :static true}
  [s] (clojure.lang.RT/readString s))
Vars in clojure.core/read-string:
Used in 0 other vars

Comments top

No comments for read-string. Log in to add a comment.