nev*_*glu 0 lisp string trim common-lisp
为什么字符串修剪对 Common Lisp 中的全局变量不起作用?
(defvar *whitespaces* '(#\Space #\Newline #\Backspace #\Tab
#\Linefeed #\Page #\Return #\Rubout))
(defvar *str* "Hello World")
(defun trim (s)
(string-trim *whitespaces* s))
(print (trim *str*))
;; output "Hello World"
Run Code Online (Sandbox Code Playgroud)
正如手册中所说:
string-trim返回字符串的一个子字符串,其中字符包中的所有字符都被去掉了开头和结尾。
所以,
CL-USER> (defvar *str* " Hello World ")
*STR*
CL-USER> (trim *str*)
"Hello World"
Run Code Online (Sandbox Code Playgroud)
如果要删除单词之间的所有空格字符,可以使用一些库,例如cl-str:
CL-USER> (ql:quickload "str")
...
CL-USER> (str:collapse-whitespaces *str*)
"Hello World"
T
Run Code Online (Sandbox Code Playgroud)