Skip to content Skip to sidebar Skip to footer

Get last 10 characters from a string using Java script

Reading Time: < 1 minute

If you want to get last 10 or 5 or nth characters from a string using java script, this can be done very easily.substr function can be use to this.
Example:
My string is “http://www.mysite.com/blog/2012/09/”
I want to get 2012/09.This can be get like this.

var myString  = 'http://www.mysite.com/blog/2012/09/';
var newString = myString.substr(myString.length - 8);
alert(newString); //  2012/09/

Further you want to remove these Forwardslashes(/) this can be done like this.

var myString  = 'http://www.mysite.com/blog/2012/09/';
myString = myString.replace(/\/$/,'');
alert(myString);

More info Java script remove backslashes and forwardslashes from a string