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; |
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'.