How can I convert a list to string so I can call insert
or message
with it? I need to display c-offsets-alist
but I got Wrong type argument: char-or-string-p
for insert or Wrong type argument: stringp
for message.
Asked
Active
Viewed 2.2k times
5 Answers
64
I am not sure of what you are trying to achieve, but format
converts "stuff" to strings. For instance:
(format "%s" your-list)
will return a representation of your list. message
uses format internally, so
(message "%s" your-list)
will print it

juanleon
- 9,220
- 30
- 41
-
9Probably better `%S` instead of `%s`, to print the list in Lisp syntax. – Sep 24 '13 at 11:37
34
(format)
will embed parentheses in the string, e.g.:
ELISP> (format "%s" '("foo" "bar"))
"(foo bar)"
Thus if you need an analogue to Ruby/JavaScript-like join()
, there is (mapconcat)
:
ELISP> (mapconcat 'identity '("foo" "bar") " ")
"foo bar"

Alexander Gromnitsky
- 2,949
- 2
- 33
- 38
10
Or
(prin1-to-string your-string)
Finally something special
(princ your-string)

Andreas Röhler
- 4,804
- 14
- 18
0
If you need to convert a list like ((a b c d e))
to a string "a b c d e"
then this function might help:
(defun convert-list-to-string (list)
"Convert LIST to string."
(let* ((string-with-parenthesis (format "%S" list))
(end (- (length string-with-parenthesis) 2)))
(substring string-with-parenthesis 2 end)))

Evgeny Mikhaylov
- 58
- 1
- 6
-
I don't want to get rid of parentheses I want to display a list or a tree as it is. If you have a list of strings and want to convert that to sting you have other options than substring. You can create a function to convert any type of string and use `(string-join (map 'symbol-name '(foo bar baz)) " ")` here I use `symbol-name` but it should be `any->string`. – jcubic May 02 '21 at 18:38
-
And your code will make no sense if you call it like this `(convert-list-to-string '(foo bar (baz (quux))))` I think that you should ask another question because this is not the answer to my question. – jcubic May 02 '21 at 18:41
-
This is just my solution, taking the answer of Alexander Gromnitsky and getting rid of parenthesis. I stumbled upon the problem of converting a list like ````((a b c d e))```` to a string ````"a b c d e"````. This works for me and I hope will help someone else. – Evgeny Mikhaylov May 03 '21 at 07:00
-
This looks like an answer to a different question, how to convert `((a b c d e))` to a string `"a b c d e"` – jcubic May 04 '21 at 07:09
-
@jcubic The question is "How to convert list to string in Emacs Lisp?" And I found your question trying to get an answer to mine. Alexander Gromnitsky answered my question and I adapted it. And if I found your question smb may also just need my answer for their same question "How to convert list to string in Emacs Lisp?" – Evgeny Mikhaylov May 04 '21 at 07:21