System.SysUtils.TStringHelper.Replace

From RAD Studio Code Examples
Jump to: navigation, search

Description

For string handling in Delphi, we recommend that you use System.SysUtils.TStringHelper, which is supported for all target platforms. For C++, use System.SysUtils.StringReplace.

This is a simple example to show how to use the Replace function to replace all occurrences of a substring with another substring with case-sensitivity.

To build and test this example:

  1. Create a FireMonkey desktop application: File > New > FireMonkey Desktop Application - Delphi > HD FireMonkey Application.
  2. Add the following components to your form:
    • Two TMemo components with two TLabel with Text property as Text and Text Result.
    • Two TEdit components with two TLabel with Text property as String To Replace and Replace With.
    • A TButton with Text property Replace.
    • A TCheckBox with the Text property Case-sensitive.
At this point, your form should look like this:

Replace.png

Note: You could use VCL for this code example too (use File > New > VCL Forms Application - Delphi).
Using VCL, your visual components are:

Code

Add the following code to the OnClick event handler for the TButton.

In Delphi:

procedure TForm1.Button1Click(Sender: TObject);
var
  MyText: string;
  StringToReplace: string;
  ReplacementString: string;
begin
  try
    MyText := Memo1.Lines.Text;
    StringToReplace := edit2.Text;
    ReplacementString := Edit3.Text;
    // If the checkbox is selected replaces all the occurrences with case-sensitivity.
    if CheckBox1.IsChecked = True then
      Memo2.Lines.Text := MyText.Replace(StringToReplace, ReplacementString,
        [rfReplaceAll]) // Use the "rfReplaceAll" flag to replace all occurrences.
    // If the checkbox is not selected replaces all the occurrences without case-sensitivity.
    else
      Memo2.Lines.Text := MyText.Replace(StringToReplace, ReplacementString,
        [rfReplaceAll, rfIgnoreCase]);  // Use the "fIgnoreCase" flag to ignore the character case.
  except
    on E: Exception do
      ShowMessage(E.ClassName + 'error raised, with message: ' + E.Message);
  end;
end;

In C++:

Use System.SysUtils.StringReplace in C++ instead of System.SysUtils.TStringHelper.Replace (Delphi class helpers are not supported in C++).

void __fastcall TForm1::Button1Click(TObject *Sender) {
  String MyText;
  String StringToReplace;
  String ReplacementString;
  try {
      MyText = Memo1->Lines->Text;
      StringToReplace = edit2->Text;
      ReplacementString = edit3->Text;
      
      if (CheckBox1->IsChecked) {
      // If the checkbox is selected replaces all the occurrences with case-sensitivity.
        Memo2->Lines->Text = StringReplace(MyText, StringToReplace,
	  ReplacementString, TReplaceFlags() << rfReplaceAll); // Use the "rfReplaceAll" flag to replace all occurrences.
      }
      // If the checkbox is not selected replaces all the occurrences without case-sensitivity.
      else {
        Memo2->Lines->Text = StringReplace(MyText, StringToReplace,
	  ReplacementString, TReplaceFlags() << rfReplaceAll << rfIgnoreCase);  // Use the "fIgnoreCase" flag to ignore the character case.
      }
  }
  catch (Exception& e) {
    ShowMessage(e.ToString());
  }
}

Uses

See Also