elisp: find/replace in region

suppose i want write a command that do a find/replace on a region

which is more efficient?

(defun space2underscore-region (p1 p2)
"Replace space by underscore in region."
(interactive "r")
(narrow-to-region p1 p2)
(goto-char (point-min))
(while (search-forward " " nil t) (replace-match "_")) ) )


(setq meat (replace-regexp-in-string " " "_"
(buffer-substring-no-properties p1 p2)) )
(delete-region p1 p2)
(insert meat)


(replace-string " " "_" nil p1 p2)

it appears to me, the first is most efficient but is most cumbersome.
The last is most lean, but least efficient.

Xah ∑ xahlee.org ☄

No comments:

Post a Comment