DelphiBasics
StringReplace
Function
Replace one or more substrings found within a string SysUtils unit
 function StringReplace(const SourceString, OldPattern, NewPattern string; Flags TReplaceFlags):string;
Description
The StringReplace function replaces the first or all occurences of a substring OldPattern in SourceString with NewPattern according to Flags settings.
 
The changed string is returned.
 
The Flags may be none, one, or both of these set values:
 
rfReplaceAll  : Change all occurrences
rfIgnoreCase  : Ignore case when searching

 
These values are specified in square brackets, as in the example.
Related commands
AnsiReplaceStrReplaces a part of one string with another
ConcatConcatenates one or more strings into one string
CopyCreate a copy of part of a string or an array
DeleteDelete a section of characters from a string
InsertInsert a string into another string
MoveCopy bytes of data from a source to a destination
StuffStringReplaces a part of one string with another
WrapTextAdd line feeds into a string to simulate word wrap
 Download this web site as a Windows program.




 
Example code : Change a to THE in a sentence
var
  before, after : string;

begin
  // Try to replace all occurrences of a or A to THE
  before := 'This is a way to live A big life';

  after  := StringReplace(before, ' a ', ' THE ',
                          [rfReplaceAll, rfIgnoreCase]);
  ShowMessage('Before = '+before);
  ShowMessage('After  = '+after);
end;
Show full unit code
  Before = This is a way to live A big life
  After = This is THE way to live THE big life
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page