Clojure长文字字符串 我想要什么
某些编程语言具有创建多行文字字符串的功能,例如:Clojure长文字字符串 我想要什么,clojure,Clojure,某些编程语言具有创建多行文字字符串的功能,例如: some stuff ... <<EOF this is all part of the string as is this \ is a literal slash \n is a literal \ followed by a literal n the string ends on the next line EOF "hello \\ there \\ world " => "hello \\\n
some stuff ... <<EOF
this is all part of the string
as is this
\ is a literal slash
\n is a literal \ followed by a literal n
the string ends on the next line
EOF
"hello \\
there \\
world "
=> "hello \\\nthere \\\nworld"
一些东西 如果字符串中需要\
字符,只需将其转义即可。您无需执行任何其他操作即可支持多行字符串,例如:
some stuff ... <<EOF
this is all part of the string
as is this
\ is a literal slash
\n is a literal \ followed by a literal n
the string ends on the next line
EOF
"hello \\
there \\
world "
=> "hello \\\nthere \\\nworld"
编辑:
既然您已经澄清了您不想转义\
字符,那么我担心Clojure没有提供转义字符的特殊语法,这就是问题所在。本质上,Clojure处理的字符串语法与Java相同,没有特殊的语法可用。您可能对这个小函数感兴趣
(defn long-str [& strings] (clojure.string/join "\n" strings))
(defn my-string [] (slurp "something.sql"))
你会这样用的
(long-str "This is the first line. Implicitly I want a new line"
"When I put a new line in the function call to create a new line")
这确实需要额外的双引号,但更接近您想要的内容。如果您真的想这样做,请将文字字符串放入txt文件中,并使用clojure将其清除
(def my-string (slurp "super_long_string.txt"))
如果你想让它成为热插拔的,就在函数中使用它
(defn long-str [& strings] (clojure.string/join "\n" strings))
(defn my-string [] (slurp "something.sql"))
这样,每次更改原始字符串时,您都可以通过调用(我的字符串)
在程序中自动获取其当前值,关键是不必转义\字符。我想要一个文本的heredoc。如果你不在乎必须转义\,那么多行“已经做了你似乎想要做的事情。当有str
时,为什么你需要创建一个新函数?例如,(str“这里有一行。\n”和第二行。)
@celwell避免像您的示例中那样显式包含新行字符。由于目标是生成逻辑多行,\n这很烦人。Clojure应该支持一种``样式,允许像Go中那样跨行中断。