[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to count the number of occurrences of a character in a string?
From: |
Kaushal Modi |
Subject: |
Re: How to count the number of occurrences of a character in a string? |
Date: |
Tue, 13 Oct 2015 12:06:36 -0400 |
Thanks Nick, Stefan for the feedback.
Here's the updated code just to fix the issues you pointed out (with a
little "test-suite" at the end of that progn form) :
(progn
(defun my/count-char-in-string (char str)
"Count the number of times CHAR character appears in STR string."
(message "\n==========\nstr = %0s" str)
(let* ((num-matches 0)
(ptr 0) ; initiate pointer for string match
match-pos)
(while (<= ptr (length str))
(message "ptr = %0d" ptr)
(setq match-pos (string-match-p
(regexp-quote (char-to-string char)) str ptr))
(if match-pos
(progn
(setq ptr (1+ match-pos))
(message "match-pos = %0d ptr = %0d" match-pos ptr)
(setq num-matches (1+ num-matches)))
(progn
(setq ptr (1+ ptr)))))
(message "%0d occurrence%0s of `%c' char found in \"%s\"."
num-matches (if (/= 1 num-matches) "s" "") char str)
num-matches))
(my/count-char-in-string ?a "abcda")
(my/count-char-in-string ?z "abcda")
(my/count-char-in-string ?f "falalala")
(my/count-char-in-string ?^ "f^la^lala^dabra^^")
(my/count-char-in-string ?\\ "\\falalala\\"))
--
Kaushal Modi
On Mon, Oct 12, 2015 at 9:18 PM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>> (setq str (substring-no-properties str (1+ match-pos)))
>
> This will give wrong results for regexp that use things like ^ and \`.
>
> Instead, you should keep using the same `str' and instead pass the
> `start' arg to string-match.
>
>
> Stefan
>
>
- How to count the number of occurrences of a character in a string?, Marcin Borkowski, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Nick Dokos, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Stefan Monnier, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?,
Kaushal Modi <=
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/15
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/15
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/15
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Message not available
- Re: How to count the number of occurrences of a character in a string?, Brendan Halpin, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13