Something like:

  // ----------------------------------------------------
  // Project method : str_Occurences
  // Database: Sandbox
  // ID[3FBA4FA5A1B74C9A87061DA1D97D5E51]
  // Created #2-1-2015 by Vincent de Lachaux
  // ----------------------------------------------------
  // Description:
  //
  // ----------------------------------------------------
  // Declarations
C_LONGINT($0)
C_TEXT($1)
C_TEXT($2)
C_BOOLEAN($3)

C_BOOLEAN($Boo_diacritical)
C_LONGINT($Lon_afterLength;$Lon_beforeLength;$Lon_delta;$Lon_occurences;$Lon_parameters)
C_TEXT($Txt_match;$Txt_target)

If (False)
C_LONGINT(str_Occurences ;$0)
C_TEXT(str_Occurences ;$1)
C_TEXT(str_Occurences ;$2)
C_BOOLEAN(str_Occurences ;$3)
End if

  // unitTest:
If (False)
ASSERT(str_Occurences ("Hello world!";"l")=3)
ASSERT(str_Occurences ("Hello world!";"Hello")=1)
ASSERT(str_Occurences ("Hello world!";"hello")=0)
ASSERT(str_Occurences ("Hello world!";"hello";False)=1)

End if
  // ----------------------------------------------------
  // Initialisations
$Lon_parameters:=Count parameters

If (Asserted($Lon_parameters>=2;"Missing parameter"))

  //Required parameters
$Txt_target:=$1
$Txt_match:=$2

$Boo_diacritical:=True

  //Optional parameters
If ($Lon_parameters>=3)

$Boo_diacritical:=$3

End if

Else

ABORT

End if

  // ----------------------------------------------------

$Lon_beforeLength:=Length($Txt_target)

If ($Boo_diacritical)

  //comparisons will be based on character codes
$Txt_target:=Replace string($Txt_target;$Txt_match;"";*)

Else

$Txt_target:=Replace string($Txt_target;$Txt_match;"")

End if

$Lon_afterLength:=Length($Txt_target)

$Lon_delta:=$Lon_beforeLength-$Lon_afterLength

$Lon_occurences:=Choose(Length($Txt_match)=1;$Lon_delta;$Lon_delta/Length($Txt_match))

  // ----------------------------------------------------
  // Return
$0:=$Lon_occurences

  // ----------------------------------------------------
// End


v i n c e n t    d e    l a c h a u x
----------------------------------------
Bee green - keep it on the screen



**********************************************************************
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]
**********************************************************************

Reply via email to