Function: replace-string-in-region
replace-string-in-region is a byte-compiled function defined in
subr.el.gz.
Signature
(replace-string-in-region STRING REPLACEMENT &optional START END)
Documentation
Replace STRING with REPLACEMENT in the region from START to END.
The number of replaced occurrences are returned, or nil if STRING doesn't exist in the region.
If START is nil, use the current point. If END is nil, use point-max.
Comparisons and replacements are done with fixed case.
Other relevant functions are documented in the buffer group.
Probably introduced at or before Emacs version 28.1.
Shortdoc
;; buffer
(replace-string-in-region "foo" "bar")
Source Code
;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun replace-string-in-region (string replacement &optional start end)
"Replace STRING with REPLACEMENT in the region from START to END.
The number of replaced occurrences are returned, or nil if STRING
doesn't exist in the region.
If START is nil, use the current point. If END is nil, use `point-max'.
Comparisons and replacements are done with fixed case."
(if start
(when (< start (point-min))
(error "Start before start of buffer"))
(setq start (point)))
(if end
(when (> end (point-max))
(error "End after end of buffer"))
(setq end (point-max)))
(save-excursion
(goto-char start)
(save-restriction
(narrow-to-region start end)
(let ((matches 0)
(case-fold-search nil))
(while (search-forward string nil t)
(delete-region (match-beginning 0) (match-end 0))
(insert replacement)
(setq matches (1+ matches)))
(and (not (zerop matches))
matches)))))