System.SysUtils.TStringHelper.Replace

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

function Replace(OldChar: Char; NewChar: Char): string; overload;
function Replace(OldChar: Char; NewChar: Char; ReplaceFlags: TReplaceFlags): string; overload;
function Replace(const OldValue: string; const NewValue: string): string; overload;
function Replace(const OldValue: string; const NewValue: string; ReplaceFlags: TReplaceFlags): string; overload;

Properties

Type Visibility Source Unit Parent
function public System.SysUtils.pas System.SysUtils TStringHelper

Description

Replaces an old character or string with a new given character or string.

var
  MyString: String;

begin
  MyString := 'This is a string.';

  Writeln(MyString.Replace('a', 'one'));
  Writeln(MyString.Replace('a', '1'));
end.

Output:

This is one string.
This is 1 string.

There are four Replace overloaded methods. The first two replace only characters, while the third and fourth replace strings.

The ReplaceFlags parameter of type TReplaceFlags is introduced for you to use any of these two flags:

  • rfReplaceAll: Replace all occurrences. If this flag is not present, only the first occurrence of the target substring is replaced.
  • rfIgnoreCase: Match occurrences of the substring case-insensitively. If this flag is not present, only case-sensitive matches are considered.

See Also