To get the last N characters of a given string in Perl, you can use negative indexing or the `substr` function.
In this example,
$str1
with a string value of "Hello World"
.substr($str1, -5)
to get the last 5 characters.$str1 = "Hello World";
$last5Chars = substr($str1, -5);
print "Last 5 characters of the string are: $last5Chars\n";
Last 5 characters of the string are: World
In this example,
$str2
with a string value of "apple"
.substr($str2, -3)
to get the last 3 characters.$str2 = "apple";
$last3Chars = substr($str2, -3);
print "Last 3 characters of the string are: $last3Chars\n";
Last 3 characters of the string are: ple
In this tutorial, we learned How to get Last N Characters in String in Perl language with well detailed examples.