Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

LastPos function - last occurrence of one string within another

By Zarko Gajic, About.com

Here's an "extension" function for the RTL Pos function. The LastPos returns an integer specifying the position of the LAST occurrence of one string within another.

~~~~~~~~~~~~~~~~~~~~~~~~~
function LastPos(const SubStr: String; const S: String): Integer;
begin
   result := Pos(Reverse(SubStr), Reverse(S)) ;

   if (result <> 0) then
     result := ((Length(S) - Length(SubStr)) + 1) - result + 1;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Setting up application wide hot key (keyboard short cut)
« How to encrypt a string in Delphi for .NET

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2004 Delphi Tips
  7. LastPos function - last occurrence of one string within another

©2009 About.com, a part of The New York Times Company.

All rights reserved.