Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure Long Literal String

Tags:

clojure

What I want

Some programming languages have a feature for creating multi-line literal strings, for example:

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

Question: Does Clojure have something similar to this? I realize that " handles multi-line fine, but I want it to also properly handle \ as a literal.

Thanks!

like image 827
user1383359 Avatar asked Jun 16 '12 15:06

user1383359


2 Answers

You might be interested in this little function.

(defn long-str [& strings] (clojure.string/join "\n" strings))

You'd use like so

(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")

This does require extra double quotes, but would be closer to what you want.

like image 98
Virmundi Avatar answered Nov 03 '22 21:11

Virmundi


If you need a \ character in the string, just escape it. You don't have to do anything additional to support multiline strings, for example:

"hello \\
there \\
world "

=> "hello \\\nthere \\\nworld"

EDIT :

Now that you've clarified that you don't want to escape the \ character, I'm afraid that Clojure doesn't offer a special syntax for escaping characters, this has been asked before. In essence, Clojure deals with the same string syntax as Java, with no special syntax available.

like image 13
Óscar López Avatar answered Nov 03 '22 20:11

Óscar López