escape characters to make a string literal

659 views
Skip to first unread message

Brian Craft

unread,
Mar 29, 2017, 1:33:01 PM3/29/17
to Clojure
Is there an easy way to display a string as a string literal that can be read by clojure?

E.g.

> (let [x "\001"] (what-goes-here x))
"\001"

Alex Miller

unread,
Mar 29, 2017, 2:02:40 PM3/29/17
to Clojure
Clojure leans on Java to read that literal. There is no Clojure function to forcibly print it that way again, but you can probably use the Java methods on String or Character to get the String representation of a character somehow.

Brian Craft

unread,
Mar 29, 2017, 2:49:45 PM3/29/17
to Clojure
A funny hole in the meta-programming story. I don't think the String or Character classes have methods that do this.

Also interesting, clojure's print methods will handle some escapes, but not others:

cavm.core=> (println (pr-str "foo\tbar"))

"foo\tbar"

nil

cavm.core=> (println (pr-str "foo\001bar"))

"foobar"

nil



Apparently org.apache.commons.lang3.StringEscapeUtils.escapeJava is one solution.

Didier

unread,
Mar 29, 2017, 7:28:38 PM3/29/17
to Clojure
| Also interesting, clojure's print methods will handle some escapes, but not others

I think that's just that \t is printed as \t. Like a tab shows up as \t. Whereas say a Unicode like \u1234 will show the character of it. You can seek that here:

(print (pr-str "foo\u0009bar"))
"foo\tbar"

Also, what is \001? I would have expected only the first 0 to be escaped. Is this a Java special escape syntax?

Christophe Grand

unread,
Mar 30, 2017, 5:13:23 AM3/30/17
to clojure
A funny thing is that octal or hexadecimal escapes are not part of EDN (even if most readers recognize them).

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting & Contracting http://lambdanext.eu/

Brian Craft

unread,
Mar 30, 2017, 10:56:52 PM3/30/17
to Clojure, chris...@cgrand.net
The specification seems ambiguous on this point. The definition of strings doesn't mention hex or octal escapes, but the definition of characters does. No relationship between characters and strings is specified. Characters are unicode, apparently; strings are unspecified.

On Thursday, March 30, 2017 at 2:13:23 AM UTC-7, Christophe Grand wrote:
A funny thing is that octal or hexadecimal escapes are not part of EDN (even if most readers recognize them).
On Wed, Mar 29, 2017 at 8:49 PM, Brian Craft <craft...@gmail.com> wrote:
A funny hole in the meta-programming story. I don't think the String or Character classes have methods that do this.

Also interesting, clojure's print methods will handle some escapes, but not others:

cavm.core=> (println (pr-str "foo\tbar"))

"foo\tbar"

nil

cavm.core=> (println (pr-str "foo\001bar"))

"foobar"

nil



Apparently org.apache.commons.lang3.StringEscapeUtils.escapeJava is one solution.


On Wednesday, March 29, 2017 at 11:02:40 AM UTC-7, Alex Miller wrote:
Clojure leans on Java to read that literal. There is no Clojure function to forcibly print it that way again, but you can probably use the Java methods on String or Character to get the String representation of a character somehow.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages