Common Lisp 中的 str_replace?

发布于 2024-10-05 22:16:55 字数 198 浏览 0 评论 0原文

Common Lisp 中是否有类似于 PHP 的 str_replace 的函数?

http://php.net/manual/en/function.str-replace。 php

Is there some function similar to PHP's str_replace in Common Lisp?

http://php.net/manual/en/function.str-replace.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温折酒 2024-10-12 22:16:55

有一个名为 cl-ppcre 的库:

(cl-ppcre:regex-replace-all "qwer" "something to qwer" "replace")
; "something to replace"

通过 quicklisp 安装它。

There is a library called cl-ppcre:

(cl-ppcre:regex-replace-all "qwer" "something to qwer" "replace")
; "something to replace"

Install it via quicklisp.

草莓味的萝莉 2024-10-12 22:16:55

如果替换只有一个字符(这种情况很常见),您可以使用 Replace:

(substitute #\+ #\Space "a simple example") => "a+simple+example"

If the replacement is only one character, which is often the case, you can use substitute:

(substitute #\+ #\Space "a simple example") => "a+simple+example"
长伴 2024-10-12 22:16:55

我认为标准中没有这样的功能。如果您不想使用正则表达式 (cl-ppcre),您可以使用:

(defun string-replace (search replace string &optional count)
  (loop for start = (search search (or result string)
                            :start2 (if start (1+ start) 0))
        while (and start
                   (or (null count) (> count 0)))
        for result = (concatenate 'string
                                  (subseq (or result string) 0 start)
                                  replace
                                  (subseq (or result string)
                                          (+ start (length search))))
        do (when count (decf count))
        finally (return-from string-replace (or result string))))

编辑: Shin Aoyama 指出这不适用于替换,例如 "\" ""str\"ing" 中的 "\\\""。由于我现在认为上述内容相当麻烦,所以我应该建议中给出的实现Common Lisp Cookbook,其中好多了:

(defun replace-all (string part replacement &key (test #'char=))
  "Returns a new string in which all the occurences of the part 
is replaced with replacement."
  (with-output-to-string (out)
    (loop with part-length = (length part)
          for old-pos = 0 then (+ pos part-length)
          for pos = (search part string
                            :start2 old-pos
                            :test test)
          do (write-string string out
                           :start old-pos
                           :end (or pos (length string)))
          when pos do (write-string replacement out)
          while pos)))

我特别喜欢使用 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:

(defun string-replace (search replace string &optional count)
  (loop for start = (search search (or result string)
                            :start2 (if start (1+ start) 0))
        while (and start
                   (or (null count) (> count 0)))
        for result = (concatenate 'string
                                  (subseq (or result string) 0 start)
                                  replace
                                  (subseq (or result string)
                                          (+ start (length search))))
        do (when count (decf count))
        finally (return-from string-replace (or result string))))

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:

(defun replace-all (string part replacement &key (test #'char=))
  "Returns a new string in which all the occurences of the part 
is replaced with replacement."
  (with-output-to-string (out)
    (loop with part-length = (length part)
          for old-pos = 0 then (+ pos part-length)
          for pos = (search part string
                            :start2 old-pos
                            :test test)
          do (write-string string out
                           :start old-pos
                           :end (or pos (length string)))
          when pos do (write-string replacement out)
          while pos)))

I especially like the use of with-output-to-string, which generally performs better than concatenate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文