|
From: | Patrick McAllister |
Subject: | Re: count char in string [The Emacs Challenge Competition - round 2, with fallout] |
Date: | Thu, 29 Oct 2020 14:53:12 +0100 |
User-agent: | mu4e 1.4.13; emacs 26.3 |
On Wed, Oct 28 2020, Emanuel Berg via Users list for the GNU Emacs text editor wrote: > round 2: > > Very simple: we are looking for a function that returns the number of > occurrences of a char in a string. > > Emanuel Berg's solution: > > (require 'cl-lib) > (defun count-char-in-string (the-char str) > (let ((c (if (characterp the-char) the-char (string-to-char the-char))) > (cs (string-to-list str)) ) > (cl-count c cs) )) > ;; (count-char-in-string ?a "Emacs skills kills") ; 1 I had this lying around: (defun count-char-in-string (char string) "Count CHARS in STRING (after normalizing it)." (length (cl-remove-if-not (lambda (x) (= char x)) (append (ucs-normalize-NFKC-string string) nil)))) -- Patrick
[Prev in Thread] | Current Thread | [Next in Thread] |