Replace one or more substrings found within a string SysUtils unit
 function StringReplace ( const SourceString, OldPattern, NewPattern : string; Flags : TReplaceFlags ) : string;
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
AnsiReplaceStr Replaces a part of one string with another
Concat Concatenates one or more strings into one string
Copy Create a copy of part of a string or an array
Delete Delete a section of characters from a string
Insert Insert a string into another string
Move Copy bytes of data from a source to a destination
StuffString Replaces a part of one string with another
WrapText Add line feeds into a string to simulate word wrap
 Author links

 Download this web site as a Windows program.

Example code : Change a to THE in a sentence
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
  SysUtils,   // Unit containing the StringReplace command
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  before, after : string;

  // 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);
Hide 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 2002 - 2020. All rights reserved.  |  Home Page