简体   繁体   中英

How to convert list to string in Emacs Lisp

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.

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

(format) will embed parentheses in the string, eg:

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"

Or

(prin1-to-string your-string)

Finally something special

(princ your-string)
M-x pp-eval-expression RET c-offsets-alist RET

If you need to convert a list like ((abcde)) to a string "abcde" 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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM