in reply to Re: Replace the last letter
in thread Replace the last letter

Personally I'd go down the regex route too for preference. If the find/replace chars that you're using are always the same then you can improve the effeciency with just:

$string = 'Snakes'; $string =~ s/s$/X/;

If you want to take the substr() route then try:

substr($string,-1,1,'X');

That will replace the last character whatever it is with the X. For more details I'd also point you towards the perldocs - if you don't have access to the perldoc function then look it up on http://www.perldoc.com.

--- Jay

All code is untested unless otherwise stated.

Replies are listed 'Best First'.
Re^3: Replace the last letter
by radiantmatrix (Parson) on Oct 07, 2004 at 18:13 UTC

    If you always want the last letter, you could match:

    $string =~ s/.$/X/; ## or $letter = 'X'; $string =~ s/.$/$letter/;
    As opposed to the substr use.

    radiantmatrix
    require General::Disclaimer;