Clojure嵌入var到String,另一种方法?喜欢sprintf方式吗? [英] Clojure embed var to String, a alternative way? Like sprintf way?

查看:110
本文介绍了Clojure嵌入var到String,另一种方法?喜欢sprintf方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道这是用变量构造一个字符串的正确方法。
但是有类似的方式吗?

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

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

我试过,这是无效的语法, Clojure?

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

感谢。

推荐答案

Diego Basch提到 format ,这是一个很好的答案。您还可以使用 cl-format

Diego Basch mentioned format, which is a good answer. You can also use cl-format:

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

nil 作为第二个参数,返回一个字符串。第二个参数的其他选项将导致字符串写入某处。

With nil as the second argument, this returns a string. Other options for the second argument will cause the string to be written somewhere.

格式使用 java.lang.String.format cl-format 是Common Lisp的格式的Clojure版本。对于格式化字符串,它们的功能非常相似,但有一些事情, cl-format 可以很好地Clojure的格式不。简单示例 - 打印 nil

format uses java.lang.String.format. cl-format is a Clojure version of Common Lisp's format. For formatting strings, their functionality is very similar, but there are a few things that cl-format can do well that Clojure's format doesn't. Simple example--printing 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."

这篇关于Clojure嵌入var到String,另一种方法?喜欢sprintf方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆