Wednesday, March 2, 2011

Search and replace

Almost every text editor / word processor has the ability to search for a given string and replace it with another. If you're planning on adding similar functionality to your application, here's an example of where all of it can start:

function SearchAndReplace(
  sSrc, sLookFor, sReplaceWith
    : string )
      : string;
  nLenLookFor : integer;
  nPos        := Pos( sLookFor, sSrc );
  nLenLookFor := Length( sLookFor );
  while(nPos > 0)do
    Delete( sSrc, nPos, nLenLookFor );
    Insert( sReplaceWith, sSrc, nPos );
    nPos := Pos( sLookFor, sSrc );
  Result := sSrc;
For example, let's say you have a string -- 'this,is,a,test' -- and you want to replace the commas with spaces. Here's how you'd call SearchAndReplace():
SearchAndReplace( 'this,is,a,test', ',', ' ' )
SearchAndReplace() will now return the string 'this is a test'.

Source :

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum