KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: String Replacement Method for 4D Write Pro
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: September 1, 2020

Below is a method that can be used to replace WR Replace when replacing 4D Write with 4D Write Pro.
The method uses WP Get text and Position to find a specific string in the 4D Write Pro document referenced by a pointer. An optional pointer to a related 4D Write Pro Range object can be passed to tell the method where to perform the search.

// Method: WP_Replace
C_POINTER($wpArea_p;$1)
C_TEXT($oldStr_t;$2)
C_TEXT($newStr_t;$3)
C_POINTER($4)

If (Count parameters>2)
   $wpArea_p:=$1
   $oldStr_t:=$2
   $newStr_t:=$3
  
   C_LONGINT($start_l;$pos_l)
   C_TEXT($text_t)
   C_OBJECT($wpRange_o)
  
   $text:=WP Get text($wpArea_p->)
  
   If (Count parameters=4)
      $wpRange_o:=OB Copy($4->)
   Else
      $wpRange_o:=WP Text range($wpArea_p->;wk start text;wk start text)
   End if

   $start_l:=$wpRange_o.end
  
   $pos_l:=Position($oldStr_t;$text;$start_l)
  
   If ($pos_l>0)
      $wpRange_o:=WP Text range($wpArea_p->;$pos_l;$pos_l+Length($oldStr_t))
      WP SET TEXT($wpRange_o;$newStr_t;wk replace)
   End if
End if


Example of using the method:
WP_Replace(->$wpDoc_ob;"Morning";"Afternoon")

The example above will find the first instance of the word "Morning" from the start of the document and replace it with "Afternoon".