Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Parse question

From   n j cox <>
Subject   Re: st: Parse question
Date   Thu, 22 Sep 2005 15:57:50 +0100

I was wondering this the other day but didn't
remember anything.

Given this,

*! NJC 1.0.0 22 Sept 2005
program charcount
version 8
args string char garbage
if `"`char'"' == "" | `"`garbage'"' != "" error 198

local c = 0
forval i = 1/`= length(`"`string'"')' {
if substr(`"`string'"',`i',1) == `"`char'"' local ++c

c_local count = `c'

you should fire at it a quoted string and a quoted character,

charcount "`loc'" "a"

and it will insinuate a local macro -count- in your vicinity
with the answer.

di `count'

(It should probably be sclass, and I'll attend to that in due course.)

So watch out for your own use of -count-.

There's also no doubt a Mata solution.


Is there any way to count the number of occurence of a particular character in a
local or
global macro?
For example:
local loc (a mama baba).
I would like to count the number of occurences of "a" , in this case 5.
Or local loc "There; That; This)
I want to count the number of occurences of ";".
I am writing an ado for a work and I can't figure how to do it.
I have a look at macrolists but there is no mention of that case.

* For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index