PosR function
String / Filename routines
Return to Introduction  Previous page  Next page
Unit
acUtils  

Declaration
function PosR(const SubStr, Str: String): Integer;  

Description
The PosR function searches for a substring in a string, from right side of the string, unlike standard Pos function.  
 
PosR searches for the LAST occurence of SubStr within Str (beginning from right side of Str), and returns an integer value that is the index of the first character of SubStr within Str. If SubStr is not found, PosR returns zero.  

Original code
function PosR(const SubStr, Str: String): Integer;  
var  
  I: Integer;  
begin  
  if (Length(SubStr) <> 0and (Length(Str) >= Length(SubStr)) then  
   for I := Length(Str) - Length(SubStr) downto 0 do  
    if Copy(Str, I, Length(SubStr)) = SubStr then  
     begin  
      Result := I;  
      Exit;  
     end;  
  Result := 0;  
end;  

See also
LeftPart, RightPart, LeftPartR, RightPartR and SplitStr routines.