1. Computing

Search And Replace

By

Let's say you have a string 'testing;search;and;replace' and you want to replace ';' with spaces. Here's how you'd call this function:

sOldString:='testing;search;and;replace';
sNewString:=SearchAndReplace(sOldString, ';', ' ')

~~~~~~~~~~~~~~~~~~~~~~~~~
function SearchAndReplace
   (sSrc, sLookFor, sReplaceWith : string) : string;
var
   nPos, nLenLookFor : integer;
begin
   nPos := Pos(sLookFor, sSrc) ;
   nLenLookFor := Length(sLookFor) ;
   while (nPos > 0) do begin
     Delete(sSrc, nPos, nLenLookFor) ;
     Insert(sReplaceWith, sSrc, nPos) ;
     nPos := Pos(sLookFor, sSrc) ;
   end;
   Result := sSrc;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
p.s.
Or, if you have Delphi 6+, you an use the StringReplace RTL function.

Delphi tips navigator:
» Convert The First Letter In Any Word To A Capital
« Reverse a String

©2014 About.com. All rights reserved.