Posted By

bigfaceworm on 08/21/09


Tagged

String count emacs LISP


Versions (?)

count-string-matches


 / Published in: Emacs Lisp
 

URL: http://stackoverflow.com/questions/41522/tips-for-learning-elisp/59589#59589

An answer to the 'count-string-matches exercise listed here: http://stackoverflow.com/questions/41522/tips-for-learning-elisp/59589#59589

  1. (defun count-string-matches (strn)
  2. "Return number of matches STRING following the point.
  3. Continues until end of buffer. Also display the count as a message."
  4. (interactive (list (read-string "Enter string: ")))
  5. (save-excursion
  6. (let ((count -1))
  7. (while
  8. (progn
  9. (setq count (1+ count))
  10. (search-forward strn nil t)))
  11. (message "%d matches" count)
  12. count)))

Report this snippet  

You need to login to post a comment.