converting a string to a list

1,628 views
Skip to first unread message

Samuel Lê

unread,
Jan 14, 2012, 5:13:59 AM1/14/12
to clo...@googlegroups.com
Hi,

I was wondering if there was a function to convert a list into a string, something like:
(string-to-list "abcde")   ;; (a b c d e)

thanks!

Sam

Bruce Durling

unread,
Jan 14, 2012, 12:57:56 PM1/14/12
to clo...@googlegroups.com
Sam,

Strings can be turned into sequences with seq.

> (seq "foo")
(\f \o \o)

The backslashes are because f o and o are character literals.

cheers,
Bruce

> --
> 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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

dennis zhuang

unread,
Jan 14, 2012, 1:01:39 PM1/14/12
to clo...@googlegroups.com
You can do this
> ((comp #(map str %) seq) "abcdef")
("a" "b" "c" "d" "e" "f")

2012/1/15 Bruce Durling <b...@otfrom.com>



--
庄晓丹
Email:        killm...@gmail.com
伯岩(花名)  bo...@taobao.com
Site:           http://fnil.net

淘宝(中国)软件有限公司 / 产品技术部 / Java中间件

Jay Fields

unread,
Jan 14, 2012, 1:13:46 PM1/14/12
to clo...@googlegroups.com
this seems easier...

user=> (map str "abc")
("a" "b" "c")

Though the original question says a list to a string and the example shows a string to a list (of symbols)

user=> (apply str ["a" "b" "c"])
"abc'

But, the example is looks like it wants

user=> (map (comp symbol str) "abc")
(a b c)

Hopefully one of those is what you're looking for.

Cheers, Jay

Andy Fingerhut

unread,
Jan 15, 2012, 2:09:10 AM1/15/12
to clo...@googlegroups.com
This may not be important for your application, but if what you want in the returned sequence are strings, and if you expect to deal with Unicode characters that are not in the Basic Multilingual Plane (BMP) set, then note the following differences.

(map str s) will return a separate string for each UTF-16 code unit, so any supplementary characters will result in 2 strings in the result, not one.  Those 2 resulting strings won't be complete Unicode strings by themselves, since they each only contain one surrogate character. [1] has more info on UTF-16 encoding if you are curious.

e.g. (map str "smile \ud83d\ude03") will contain two strings "\ud83d" and "\ude03" at the end of the resulting sequence

(re-seq #"." s) will return a separate string for each Unicode code point, so BMP or supplementary characters will each result in one string in the output sequence, where supplementary characters become strings with 2 Java chars in them.

e.g. (re-seq #'." "smile \ud83d\ude03") will contain one string "\ud83d\ude03" at the end of the resulting sequence, since those two are a surrogate pair representing a single Unicode character

Similarly, there is the notion of combining characters in Unicode [2], e.g. a with a grave accent can be represented either with a single Unicode code point, or it can be represented by the Unicode character for "a" followed by the Unicode character for a combining grave accent, U+0300.   (re-seq #'." s) would return separate strings for the "a" and the combining character in that case.  If you want each non-combining character plus any following combining characters to be kept together, you can use:

(re-seq #"\PM\pM*" s)

\pM matches any Unicode combining character (whether it is in the BMP or supplementary), and \PM matches any other Unicode character.

Andy

[1] http://en.wikipedia.org/wiki/UTF-16

[2] http://en.wikipedia.org/wiki/Unicode#Ready-made_versus_composite_characters

Samuel Lê

unread,
Jan 14, 2012, 3:07:45 PM1/14/12
to clo...@googlegroups.com
Thanks for the answsers!
Jay's last answer is just what I was looking for.

thanks,
Sam

2012/1/14 Jay Fields <j...@jayfields.com>
Reply all
Reply to author
Forward
0 new messages