Logo

Programming-Idioms

History of Idiom 296 > diff from v4 to v5

Edit summary for version 5 by Bart:
New Pascal implementation by user [Bart]

Version 4

2022-02-17, 17:22:56

Version 5

2022-02-17, 17:54:32

Idiom #296 Replace last occurrence of substring

Assign to x2 the value of string x with the last occurrence of y replaced by z.
If y is not contained in x, then x2 has the same value as x.

Idiom #296 Replace last occurrence of substring

Assign to x2 the value of string x with the last occurrence of y replaced by z.
If y is not contained in x, then x2 has the same value as x.

Variables
x2,x,y,z
Variables
x2,x,y,z
Extra Keywords
substring substitute
Extra Keywords
substring substitute
Imports
StrUtils
Code
  x2 := x;
  p := RPos(y, x);
  if (p > 0) then
  begin
    Delete(x2, p, Length(y));
    Insert(z, x2, p);
  end;