Wednesday, November 6, 2013

How to Get The Last Character of a String Using jQuery?

You can use the .slice() method, its cross browser compatible.
var id = "example1";
id.slice(-1); //Outputs: 1
Getting the last character is easy, as you can treat strings as an array:
var id = "example2";
var lastChar = id[id.length-1]; //Outputs: 2
To get a section of a string, you can use the substr function
var id = "example3";
id.substr(id.length-1); //Outputs: 3

No comments:

Post a Comment