Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to trim the begining of a string

Status
Not open for further replies.

BryanY

MIS
Joined
Aug 18, 2001
Messages
54
Location
US
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
 
Use the substr function.

Code:
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";
 
Could also do this:
Code:
my $str = '2004';

# Last two characters
my ($last_chars) = $str =~ /(..)$/;
print $last_chars, "\n";

# Remove first two characters
$str =~ s/^..//;
print $str, "\n";

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top