简体   繁体   English

如何在 Emacs Lisp 中将列表转换为字符串

[英]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?如何将列表转换为字符串,以便我可以用它调用insertmessage 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.我需要显示c-offsets-alist但我得到了Wrong type argument: char-or-string-p用于插入的Wrong type argument: char-or-string-pWrong type argument: stringp用于消息的Wrong type argument: stringp

I am not sure of what you are trying to achieve, but format converts "stuff" to strings.我不确定您要实现什么,但format将“东西”转换为字符串。 For instance:例如:

(format "%s" your-list)

will return a representation of your list.将返回您的列表的表示。 message uses format internally, so message内部使用格式,所以

(message "%s" your-list)

will print it将打印它

(format) will embed parentheses in the string, eg: (format)将在字符串中嵌入括号,例如:

ELISP> (format "%s" '("foo" "bar"))
"(foo bar)"

Thus if you need an analogue to Ruby/JavaScript-like join() , there is (mapconcat) :因此,如果您需要类似 Ruby/JavaScript 的join() ,则有(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:如果您需要将((abcde))类的列表转换为字符串"abcde"那么此函数可能会有所帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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