[Gambas-user] Counting characters

Fabien Bodard gambas.fr at ...626...
Fri Nov 11 15:58:17 CET 2011


maybe with gb.pcre : regexp

or
Public Function CountChar(sValue as string, sChar as string) as integer
dim i, iRet as integer

for i =1 to len (sValue)+1
  if mid(sValue,i,1)=sChar then inc iRet
next

return iRet
end


print CountChar("coucou ça va ?", "a")

$ 2

Or a super version thet remember a position:

Public Function GetCharPos(sValue as string, sChar as string) as integer[]
dim i as integer
dim  aRet as new integer[]

for i =1 to len (sValue)+1
  if mid(sValue,i,1)=sChar then aRet.add(i)
next

return aRet
end


print GetCharPos("Hello how are you", "e").Count


2011/11/11 Rolf-Werner Eilert <eilert-sprachen at ...221...>:
> Hi all,
>
> just a short question: Is there a function which simply counts how many
> times a character appears within a string? I vaguely remember a long
> time ago I used such a thing, but it may as well have been in another
> context.
>
> The only way I have found yet was using Split() and then count the
> number of resulting parts -1 IF it is more than 0.
>
> Any better way?
>
> Rolf
>
> ------------------------------------------------------------------------------
> RSA(R) Conference 2012
> Save $700 by Nov 18
> Register now
> http://p.sf.net/sfu/rsa-sfdev2dev1
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



-- 
Fabien Bodard




More information about the User mailing list