Clojure Insert var into String, an alternative way? How is the sprintf path?

 (str "I don't know what " (:name a) " eats."))

I know this is the right way to build a string with variables. But is there a way like this?

(str "I dont know what %s eats." (:name a))

I tried, and this is not valid syntax, but is there a similar way in Clojure?

Thank.

+2
source share
2 answers

Diego Bash said formatthat is a good answer. You can also use cl-format:

(clojure.pprint/cl-format nil "I don't know what ~a eats." "Joe")

C, the nilstring is returned as the second argument. Other parameters for the second argument will trigger a record somewhere.

format java.lang.String.format. cl-format Clojure Common Lisp format. , , cl-format , Clojure format . - nil:

(format "I don't know what %s eats." nil)
;=> "I don't know what null eats."

(clojure.pprint/cl-format nil "I don't know what ~a eats." nil)
;=> "I don't know what nil eats."
+4

format - , . :

(format "I don't know what %s eats." "Joe")

= > "I don't know what Joe eats."

+4

All Articles