last 3 chars

how do i assign a new variable to be the last three characters of a variable

ie if $old is "hello.txt"
then $new becomes "txt"

thanks
boofullsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
spreqConnect With a Mentor Commented:
1. get total character
2. simple mathematic to get location for the first of last three character
3. use substring

eg
$old = "hello.txt";
$long=length($old);   #total char
$FirstOfThree=$long-3;  #simple math
$new=substr($FirstOfThree,0,3); #get the last char



0
 
boofullsAuthor Commented:
even though there was an error in the last line....;)
0
 
ozoCommented:
$new=substr($old,-3);  #unless what you really want are all characters after the last '.'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.