KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Two utilities that will count occurrences or fields contained in a string
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: April 3, 2015

Below are two utilities that will count occurrences or fields contained in a string, one that uses 4D's Position command and one that uses the Match regex command.

Why two utilities that do the same thing?

  • There are times when it is more efficient to use Position instead of Match regex, and visa versa.

  • To demonstrate how there are often more ways than one to accomplish a task in 4D. Notice the use of Match regex requires fewer lines of code than does the use of Position.


The utilites can be used in two scenerios.
  • To count how many "occurrences" there are of a character, or group or characters, in a string.

    Given STR_CountInString ("aaa,bbb,ccc,ddd"; ",") the number 3 will be returned for how many times the comma character occurred in the string.

  • To count how many "fields" are in the string separated by a delimiting character.

    Given STR_CountInString ("aaa,bbb,ccc,ddd"; ","; True) the number 4 will be returned for how many fields are delimited by the comma character in the string.


If (True)
    If (False)
      Begin SQL
      /*
         Name: STR_CountInString
         Path: STR_CountInString

         Purpose: Count how many occurrences or fields contained in a string

         $1 - TEXT - Contains the String to be tested
         $2 - TEXT - Contains the character(s) to test with
         $3 - BOOLEAN -False = count occurances, True = count fields
         */
      End SQL
    End if
    C_TEXT($MethodName_T)
    $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
    C_LONGINT($Cnt_L)
    C_TEXT($Buf_T;$1)
    C_TEXT($Delim_T;$2)
    C_BOOLEAN($CountFields_B;$3)
    //--------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------
    //local_variable_declarations
    C_LONGINT($Ndx;$SOA;$RIS;$Cnt;$Params_L;$Pos_L;$Len_L;$Start_L)
    C_BOOLEAN($Success)
End if

//====================== Initialize & Setup ================================

$Buf_T:=$1
$Delim_T:=$2
If (Count parameters>2)
   $CountFields_B:=$3
End if

$Cnt:=0
$Start_L:=1
$Len:=Length($Delim_T)

//======================== Method Actions ==================================

If (Length($Buf_T)>0)
   $Ndx:=Position($Delim_T;$Buf_T;$Start_L)
   While ($Ndx>0)
      $Cnt:=$Cnt+1
      $Start_L:=$Ndx+$Len
      $Ndx:=Position($Delim_T;$Buf_T;$Start_L)
   End while

   If ($CountFields_B)
      $Cnt:=$Cnt+1
   End if
End if

//======================== Clean up & Exit =================================

$0:=$Cnt
//EOM




If (True)
    If (False)
      Begin SQL
      /*
         Name: STR_CountInString_Regex
         Path: STR_CountInString_Regex

         Purpose: Count how many occurrences or fields contained in a string

         $1 - TEXT - Contains the String to be tested
         $2 - TEXT - Contains the character(s) to test with
         $3 - BOOLEAN -False = count occurances, True = count fields
         */
      End SQL
    End if
    C_TEXT($MethodName_T)
    $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
    C_LONGINT($Cnt_L)
    C_TEXT($Buf_T;$1)
    C_TEXT($Delim_T;$2)
    C_BOOLEAN($CountFields_B;$3)
    //--------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------
    //local_variable_declarations
    C_LONGINT($Ndx;$SOA;$RIS;$Cnt;$Params_L;$Pos_L;$Len_L;$Start_L)
    C_BOOLEAN($Success)
End if

//====================== Initialize & Setup ================================

$Buf_T:=$1
$Delim_T:=$2
If (Count parameters>2)
   $CountFields_B:=$3
End if

$Cnt:=0
$Start_L:=1
$Len:=Length($Delim_T)

//======================== Method Actions ==================================

If (Length($Buf_T)>0)
   While (Match regex($Delim_T;$Buf_T;$Start_L;$Pos_L;$Len_L))
      $Cnt_L:=$Cnt_L+1
      $Start_L:=$Pos_L+$Len_L
   End while

   If ($CountFields_B)
      $Cnt:=$Cnt+1
   End if
End if

//======================== Clean up & Exit =================================

$0:=$Cnt
//EOM