> Le 4 mai 2017 à 14:22, Jörg Knebel via 4D_Tech <[email protected]> a écrit
> :
>
> Hi all
>
> I’m wondering if someone has a clever routine to count the number of
> appearances of a specific character/word in a string/text and is willing to
> share it.
I use this one:
//Str_count (find_t;in_t {;"*") -> long
//$3 optional: pass "*" for strict comparison (default not strict)
//example :
// $text:="eéèEÈÉ"
// $what:="e"
// $nb1:=Str_count ($what;$text) -> 6
// $nb2:=Str_count ($what;$text;"*") -> 1
C_LONGINT($0)
C_TEXT($1)
C_TEXT($2)
C_TEXT($3)
C_BOOLEAN($strict_b)
C_LONGINT($findLen_l)
C_LONGINT($len_l)
C_LONGINT($out_l)
C_LONGINT($params_l)
C_LONGINT($start_l)
C_LONGINT($textLen_l)
C_TEXT($find_t)
C_TEXT($in_t)
C_TEXT($nmc_t)
If (False)
C_LONGINT(Str_count ;$0)
C_TEXT(Str_count ;$1)
C_TEXT(Str_count ;$2)
C_TEXT(Str_count ;$3)
End if
//_
$out_l:=-1 //error
$nmc_t:=Current method name
$params_l:=Count parameters
Case of
: (Not(Asserted($params_l>1;$nmc_t+" 2 parameters required")))
//error
Else
$find_t:=$1
$in_t:=$2
$findLen_l:=Length($find_t)
$textLen_l:=Length($in_t)
$out_l:=0
$strict_b:=($params_l>2)
Case of
: ($findLen_l=0)
: ($findLen_l>$textLen_l)
Else
$start_l:=1
$len_l:=$findLen_l
If ($strict_b)
$start_l:=Position($find_t;$in_t;$start_l;*)
While ($start_l>0)
$out_l:=$out_l+1
$start_l:=$start_l+$len_l
$start_l:=Position($find_t;$in_t;$start_l;*)
End while
Else
$start_l:=Position($find_t;$in_t;$start_l;$len_l)
While ($start_l>0)
$out_l:=$out_l+1
$start_l:=$start_l+$len_l
$start_l:=Position($find_t;$in_t;$start_l;$len_l)
End while
End if
End case
End case
$0:=$out_l
//_
--
Arnaud de Montard
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ: http://lists.4d.com/faqnug.html
Archive: http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:[email protected]
**********************************************************************