How to change the last two characters of a string depending on a condition using Perl

I have a string that needs to be checked for the last two characters. If the last two characters are found that match my requirement, I need to truncate the string, otherwise leave the string as it is. I know that this can be done using control conditions, but I want to know if there is any one statement in Perl that could really achieve this. Also suggest any more effective ways to do this.

Example:

$str = "Hello";

therefore, if the last two characters say "tr", I want to truncate the string, but in this case we need to leave the string as the original, since the last characters are not "tr"

+3
source share
1

Perl s/// . \z , .

$str =~ s/tr\z//;
+6

All Articles