KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method for directional string searching
PRODUCT: 4D | VERSION: 13.3 | PLATFORM: Mac & Win
Published On: September 6, 2013

The following method performs a directional string searching to a text object. Based on the current cursor position in the target text variable/field, it searches for the next matching string for the specified direction. If the string is found, it is highlighted.

  // Method: OBJECT_FIND_STRING
  // Parameters
  //  $1 - Object Name
  //  $2 - string to search
  //  $3 - True for backward search
  //       False or omitted for forward search
  // ----------------------------------------------------


C_TEXT($1;$obj_t)
C_TEXT($2;$pat_t)
C_BOOLEAN($3;$backward_b)
C_BOOLEAN($found_b)
C_POINTER($obj_p)
C_LONGINT($start_l;$end_l;$foundat_l;$len_l)
C_LONGINT($char_l;$last_l)

If (Count parameters>=2)
  $obj_t:=$1
  $pat_t:="(?i)"+$2
  If (Count parameters>=3)
    $backward_b:=$3
  End if

  GET HIGHLIGHT(*;$obj_t;$start_l;$end_l)
  $obj_p:=OBJECT Get pointer(Object named;$obj_t)
  If ($backward_b)
    If ($start_l>1)
      $char_l:=Length($2)
      $foundat_l:=$start_l
      Repeat
        $last_l:=$foundat_l
        If ($start_l<=$char_l)
          $start_l:=1
        Else
          $start_l:=$start_l-$char_l
        End if
        $found_b:=Match regex($pat_t;$obj_p->;$start_l;$foundat_l;$len_l)
      Until (($found_b) & ($last_l#$foundat_l)) | ($start_l=1)
    End if
  Else
    $found_b:=Match regex($pat_t;$obj_p->;$end_l;$foundat_l;$len_l)
  End if
  If ($found_b)
    HIGHLIGHT TEXT(*;$obj_t;$foundat_l;$foundat_l+$len_l)
  End if
End if


For example:


OBJECT_FIND_STRING ("Variable";"4D")  // 1st execution


OBJECT_FIND_STRING ("Variable";"4D")  // 2nd execution