KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Find and count occurrences of a particular word in a 4D write pro zone
PRODUCT: 4D Write Pro | VERSION: 16 | PLATFORM: Mac & Win
Published On: June 30, 2017

This tech tip explains how to find all occurrences of a particular word and calculate the total count of the word in a 4D Write Pro area .

The method searchInWritePro will look for a particular word in the 4D write pro zone and highlight all its occurrences and calculate their number.


// --------------------------------------------------------------------------------
// Name:searchInWritePro
// Description: Method will find and calculate occurrences of a particular word in    4D write pro zone
// Input :
// $1 : Object name of 4D Write Pro Area
// $2 : Search String
// output :
// $0 : Number of a word occurences
// --------------------------------------------------------------------------------
C_LONGINT($0;$totalFound_l;$startPosition_l)
C_TEXT($1)
C_TEXT($2;$srcString_t)
C_POINTER($wpArea_p)

If (Count parameters>=2)

 $wpArea_p:=OBJECT Get pointer(Object named;$1)
 $srcString_t:=$2

 ST SET ATTRIBUTES($wpArea_p->;ST Start text;ST End text;\
 
Attribute background color;"White")

 $totalFound_l:=0
 $startPosition_l:=1
 $wpPlainText_t:=ST Get plain text($wpArea_p->)


 Repeat
  $foundPosition_l:=Position($srcString_t;$wpPlainText_t;$startPosition_l;\
  $lengthFound_l)

  If ($foundPosition_l>0)
   ST SET ATTRIBUTES($wpArea_p->;$foundPosition_l;$foundPosition_l+$lengthFound_l;\
   Attribute background color;"yellow")
   $startPosition_l:=$lengthFound_l+$foundPosition_l
   $totalFound_l:=$totalFound_l+1
  End if

 Until ($foundPosition_l=0)

End if

$0:=$totalFound_l



The example in below explains in steps how to use searchInWritePro method with a 4D write pro zone it can be used as a generic method in the project in order to use it multiple times .

Step 1 :

To set input for searchInWritePro method create a 4D Write pro zone with , a Text variable to type the searched word , a button to run the method and varibale to display the occurrences number as shown in below :




Step 2:

change the text variable name to "vFind" and the 4D write pro varibale name to "WriteProArea" and
the variable name to "compt" and put the code below in the button object method to run the searchInWritePro once is clicked :


compt:=searchInWritePro("WriteProArea";vFind)



Here is an example when looking for the word " String" in 4D write pro the occurrences are highlighted in yellow, and the "comp" variable shows 8 as the number of occurrences in the text .



Important: This tech tip works only when 4D write pro document has no background color .