Tech Tip: Utility to return the position of a character from the end of the string
PRODUCT: 4D | VERSION: 13.2 | PLATFORM: Mac & Win
Published On: February 15, 2013
The utility below will find and return the position of the last occurance of a charater or substring within a target string.
If (True) If (False) //***************************************************************************** // // STR_PositionR // // Purpose: Return the position of the last occurance // of the character or string within a string // // $0 - Longint - Position in the string // $1 - Text - What to find // $2 - Text - String to test // //***************************************************************************** End if C_TEXT($MethodName_T) $MethodName_T:=Current method name //===================== Declare Variables ================================== //method_parameters_declarations C_LONGINT($0;$PosLast_L) C_TEXT($1;$Find_T) C_TEXT($2;$String_T) //-------------------------------------------------------------------------------- //method_wide_constants_declarations //-------------------------------------------------------------------------------- //local_variable_declarations C_LONGINT($Ndx;$SOA;$RIS;$PosNext_L) C_TEXT($Marker_L) End if //====================== Initialize and Setup ================================ $Find_T:=$1 $String_T:=$2 //======================== Method Actions ================================== // Create a substitue string so target can't be found again // The length of the string has to be preserved to get true position // $Marker_L:=Char(1)*Length($Find_T) If ($Find_T=$Marker_L) $Marker_L:=Char(2)*Length($Find_T) End if // Find the target and replace it till last occurance is found // $PosLast_L:=0 Repeat $PosNext_L:=Position($Find_T;$String_T) If ($PosNext_L>0) $PosLast_L:=$PosNext_L $String_T:=Replace string($String_T;$Find_T;$Marker_L;1) End if Until ($PosNext_L=0) //======================== Clean up and Exit ================================= $0:=$PosLast_L |