Substring.Replace Method

Overload List

#SignatureDescription
1function Replace(const OldPattern, NewPattern: Substring): Substring; overload;Every non-overlapping occurrence of OldPattern replaced by NewPattern, left to right, ordinal.
2function Replace(const OldPattern, NewPattern: string): Substring; overload;
3function Replace(const OldPattern, NewPattern: string; const ReplaceFlags: TReplaceFlags): Substring; overload;Replaces OldPattern by NewPattern, ordinal: the first occurrence, or every occurrence with rfReplaceAll.

Overload 1: function Replace(const OldPattern, NewPattern: Substring): Substring; overload;

Every non-overlapping occurrence of OldPattern replaced by NewPattern, left to right, ordinal.

#NameTypeDescription
1OldPatternSubstring
2NewPatternSubstring

Returns: Substring - The new value; with an empty OldPattern or no occurrence, the text unchanged, as a part.

Remarks:

Every text kind for both patterns, and Char for single characters.

Examples

Replacing every occurrence.

uses Substr;

procedure Example;
begin
    Assert(Substring('a-b-c').Replace('-', '+') = 'a+b+c');
end;

Overload 2: function Replace(const OldPattern, NewPattern: string): Substring; overload;

#NameTypeDescription
1OldPatternstring
2NewPatternstring

Returns: Substring

Overload 3: function Replace(const OldPattern, NewPattern: string; const ReplaceFlags: TReplaceFlags): Substring; overload;

Replaces OldPattern by NewPattern, ordinal: the first occurrence, or every occurrence with rfReplaceAll.

#NameTypeDescription
1OldPatternstring
2NewPatternstring
3ReplaceFlagsTReplaceFlags

Returns: Substring - The new value.

Remarks:

Every text kind for both patterns. rfIgnoreCase raises ESubstringCaseError: ReplaceLinguistic ignores case by the locale, ReplaceAscii by A to Z only. Note: This routine compares the code units ordinally, case-sensitive and the same under every locale, and does not handle locale specific char handling, but it is 6.7 to 85 times faster than its ReplaceLinguistic alternative.

Examples

The first occurrence, and every occurrence.

uses Substr, System.SysUtils;

procedure Example;
begin
    Assert(Substring('a.b.c').Replace('.', '/', []) = 'a/b.c');
    Assert(Substring('a.b.c').Replace('.', '/', [rfReplaceAll]) = 'a/b/c');
end;
See Also: Substring.ReplaceLinguistic, Substring.ReplaceLinguistic, Substring.ReplaceAscii