Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
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
var position = x.lastIndexOf(y).clamp(0, x.length);
var x2 = x.replaceFirst(y, z, position);
x2 = x.gsub(/#{y}(?!.*#{y})/, z )
$x = 'A BB CCC this DDD EEEE this FFF';
$y = 'this';
$z = 'that';
$x2 = $x;
$pos = rindex $x, $y;
substr($x2, $pos, length($y)) = $z
unless $pos == -1;
print $x2;