Logo

Programming-Idioms

History of Idiom 296 > diff from v12 to v13

Edit summary for version 13 by tkoenig:
New Fortran implementation by user [tkoenig]

Version 12

2022-02-20, 20:05:36

Version 13

2022-03-18, 13:35:48

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
Code
  character(len=:), allocatable :: x, x2, y, z

  k = index(x,y,back=.true.)
  if (k > 0) then
     x2 = x(1:k-1) // z // x(k+len(y):)
  else
     x2 = x
  end if
Comments bubble
x and x2 are allocatable characters.