1

I am trying to learn Emacs Lisp. I would like to remove some white space from a string. I started with the following test case:

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

However, evaluating this in my Scratch buffer shows that no space is removed..

1 Answer 1

2

Here's a correction:

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

interactive is only useful for interactive command, so you don't need it here. Also take note of order of evaluation.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! This works.. So the point is that replace-regexp-in-string does not change its parameter, ss, in my case, but rather returns the modified string?
Correct. From C-h f replace-regexp-in-string: "Return a new string containing the replacements."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.