In Perl, you can use the substr
function with a negative offset to get the last character of a string.
In this example,
$str
with a value of "Hello World"
.substr
function with $str
, -1
as the offset, and 1
as the length to get the last character.$str = "Hello World";
$last_char = substr($str, -1, 1);
print "Last character of the string is: $last_char\n";
Last character of the string is: d
In this example,
$str
with a value of "apple"
.substr
function with $str
, -1
as the offset, and 1
as the length to get the last character.$str = "apple";
$last_char = substr($str, -1, 1);
print "Last character of the string is: $last_char\n";
Last character of the string is: e
In this tutorial, we learned How to get Last Character in String in Perl language with well detailed examples.