use*_*681 5 string vector clojure comma
我是Clojure的新手.我正在定义一个包含字符串值的向量.要求是从输入向量中检索由逗号分隔的String值.例如:
(def my-strings ["one" "two" "three"])
Run Code Online (Sandbox Code Playgroud)
我的预期输出应该是:
"one", "two", "three"
Run Code Online (Sandbox Code Playgroud)
我尝试过interpose,join如下图所示:
(apply str (interpose "," my-strings))
(clojure.string/join "," my-strings)
Run Code Online (Sandbox Code Playgroud)
两个都返回,"one,two,three"但我需要每个字符串包围双引号"",如上例所示.
使用map用引号将每个字符串括起来,并注意我们如何使用\"字符文字表示单引号:
(clojure.string/join "," (map #(str \" % \") my-strings))
=> "one","two","three"
Run Code Online (Sandbox Code Playgroud)
但请注意:字符串是字符内包含的文本"",但引号本身不是字符串的一部分。因此"one,two,three"输出本身并没有错误,除非您确实需要在文本周围添加额外的引号。
| 归档时间: |
|
| 查看次数: |
1220 次 |
| 最近记录: |