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
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
 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 2002 - 2014. All rights reserved.  |  Contact the author  |  Home Page