Count occurrences of a substring: Difference between revisions

Content added Content deleted
mNo edit summary
Line 1,561: Line 1,561:
(let ((substrings)) ; empty list to add occurrences of SUBSTRING as we find them
(let ((substrings)) ; empty list to add occurrences of SUBSTRING as we find them
(while (string-match substring text) ; as long as we can find SUBSTRING in TEXT
(while (string-match substring text) ; as long as we can find SUBSTRING in TEXT
(push (match-string 0 text) substrings). ; add the SUBSTRING we found to the list of substrings
(push (match-string 0 text) substrings) ; add the SUBSTRING we found to the list of substrings
(setq text (replace-match "" nil nil text))) ; remove SUBSTRING from text, and repeat while loop
(setq text (replace-match "" nil nil text))) ; remove SUBSTRING from text, and repeat while loop
(length substrings))) ; count number of items in substrings list
(length substrings))) ; count number of items in substrings list