SmartPascal
AnsiReplaceStr
Function
Replaces a part of one string with another StrUtils unit
 function AnsiReplaceStr ( const HayStack, Needle, NewNeedle : string ) : string;
Description
The AnsiReplaceStr function replaces all occurences of the Needle string in the Haystack string with a NewNeedle string.
 
It is a Case sensitive command.
Related commands
Copy Create a copy of part of a string or an array
Insert Insert a string into another string
Move Copy bytes of data from a source to a destination
StringReplace Replace one or more substrings found within a string
StuffString Replaces a part of one string with another
 
Example code : A simple example
// 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;
 
interface
 
uses
  StrUtils,   // Unit containing the AnsiReplaceStr command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  haystack : AnsiString;
begin
  haystack := 'The big cat sat on the big mat';
  ShowMessage(haystack);   // Display the haystack at the start

  // Note that AnsiReplaceStr is case sensitive
  haystack := AnsiReplaceStr(haystack, 'BIG', 'LITTLE');
  ShowMessage(haystack);   // Display the haystack now

  haystack := AnsiReplaceStr(haystack, 'big', 'little');
  ShowMessage(haystack);   // Display the haystack now
end;
 
end.
Hide full unit code
   The big cat sat on the big mat
   The big cat sat on the big mat
   The little cat sat on the little mat