如何在源代码中跨多行编写单行字符串文字

Flu*_*lux 2 string common-lisp

我想获得这个单行字符串值:

"pineapple"
Run Code Online (Sandbox Code Playgroud)

但在源代码中,我想跨多行编写字符串:

"pine
apple"
Run Code Online (Sandbox Code Playgroud)

但是,上面的多行代码将返回一个包含换行符的字符串。有没有办法阻止换行符被返回?

\在其他 Lisp 中,例如Scheme 和 Emacs Lisp,可以通过在行尾添加反斜杠来抑制换行符:

;; In Scheme and Emacs Lisp, this returns "pineapple":
"pine\
apple"
Run Code Online (Sandbox Code Playgroud)

然而,在 Common Lisp 中,在行尾放置反斜杠没有任何效果:

;; Common Lisp:
(string-equal "pine\
apple"
"pine
apple")
;; Returns: T
Run Code Online (Sandbox Code Playgroud)

如何在源代码中跨多行编写单行字符串文字?

Mar*_*ůda 5

在格式中使用 ~Newline指令

(string-equal (format nil "multi-~
line ~
pine~
apple")
"multi-line pineapple")
Run Code Online (Sandbox Code Playgroud)

  • 如果字符串是常量,也可以使用读取时评估:“#.(format ...)”。 (2认同)