What would be the best way to remove the first two characters from the begining of a string (or extract the last two characters)? In this case 2004 -> 04
my $str = '2004';
# Last two characters
my $last_chars = substr($str,-2,2);
print $last_chars, "\n";
# Remove first two characters
substr($str,0,2,'');
print $str, "\n";
my $str = '2004';
# Last two characters
my ($last_chars) = $str =~ /(..)$/;
print $last_chars, "\n";
# Remove first two characters
$str =~ s/^..//;
print $str, "\n";
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.