【问题标题】:How to capture result of replace-regexp-in-string in emacs lisp如何在emacs lisp中捕获replace-regexp-in-string的结果
【发布时间】:2013-12-03 00:32:08
【问题描述】:

我正在尝试学习 Emacs Lisp。我想从字符串中删除一些空格。我从以下测试用例开始:

(defun test-fun (str)
  (interactive)
  (let ((ss str)) 
       (replace-regexp-in-string "[ ]+" "" ss)
    (message ss)))
(test-fun "He   llo")

但是,在我的暂存缓冲区中评估它表明没有删除任何空间..

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    这里有一个更正:

    (defun test-fun (str)
      (let ((ss (replace-regexp-in-string "[ ]+" "" str)))
        (message ss)))
    

    interactive 只对交互命令有用,所以这里不需要。 还要注意评估的顺序。

    【讨论】:

    • 完美!这行得通.. 所以重点是replace-regexp-in-string 在我的情况下不会更改其参数ss,而是返回修改后的字符串?
    • 正确。来自C-h f replace-regexp-in-string:“返回一个包含替换的新字符串。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多