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

问题描述 投票:0回答:5

如何将列表转换为字符串,以便我可以用它调用

insert
message
?我需要显示
c-offsets-alist
,但我得到了
Wrong type argument: char-or-string-p
用于插入或
Wrong type argument: stringp
用于消息。

string list emacs elisp
5个回答
65
投票

我不确定你想要实现什么,但是

format
将“东西”转换为字符串。 例如:

(format "%s" your-list)

将返回您的列表的表示形式。

message
内部使用格式,所以

(message "%s" your-list)

将打印它。或者,使用

%S
而不是
%s
以 Lisp 语法打印列表。


35
投票

(format)
将在字符串中嵌入括号,例如:

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

因此,如果您需要类似于 Ruby/JavaScript 的工具

join()
,可以使用
(mapconcat)
:

ELISP> (mapconcat 'identity '("foo" "bar") " ")
"foo bar"

10
投票

或者

(prin1-to-string your-string)

终于来点特别的了

(princ your-string)

1
投票
M-x pp-eval-expression RET c-offsets-alist RET

0
投票

如果您需要将像

((a b c d e))
这样的列表转换为字符串
"a b c d e"
那么这个函数可能会有所帮助:

(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)))
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.