Count occurrences of a substring: Difference between revisions

Content added Content deleted
No edit summary
mNo edit summary
Line 1,548: Line 1,548:
<syntaxhighlight lang="emacs lisp">
<syntaxhighlight lang="emacs lisp">
(defun count-substrings (text substring)
(defun count-substrings (text substring)
"Count non-overlapping occurrences of SUBSTRING in TEXT."
(with-temp-buffer
(with-temp-buffer ; create a temporary buffer, which will be deleted when function finishes
(insert text)
(insert text) ; insert TEXT into the empty buffer
(goto-char (point-min))
(goto-char (point-min)) ; go to the beginning of the buffer
(how-many substring)))
(how-many substring))) ; count how many occurrences of SUBSTRING
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}