Common Lisp 中的 str_replace?
Common Lisp 中是否有类似于 PHP 的 str_replace 的函数?
Is there some function similar to PHP's str_replace in Common Lisp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个名为 cl-ppcre 的库:
通过 quicklisp 安装它。
There is a library called cl-ppcre:
Install it via quicklisp.
如果替换只有一个字符(这种情况很常见),您可以使用 Replace:
If the replacement is only one character, which is often the case, you can use substitute:
我认为标准中没有这样的功能。如果您不想使用正则表达式 (cl-ppcre),您可以使用:
编辑: Shin Aoyama 指出这不适用于替换,例如
"\" "
与"str\"ing"
中的"\\\""
。由于我现在认为上述内容相当麻烦,所以我应该建议中给出的实现Common Lisp Cookbook,其中好多了:我特别喜欢使用
with-output-to-string
,它通常比concatenate
性能更好。I think there is no such function in the standard. If you do not want to use a regular expression (cl-ppcre), you could use this:
EDIT: Shin Aoyama pointed out that this does not work for replacing, e.g.,
"\""
with"\\\""
in"str\"ing"
. Since I now regard the above as rather cumbersome I should propose the implementation given in the Common Lisp Cookbook, which is much better:I especially like the use of
with-output-to-string
, which generally performs better thanconcatenate
.