EDIT: As others have pointed out, use slice(-5) instead of substr. However, see the .split().pop() solution at the bottom of this answer for another approach.
Original answer:
You'll want to use the Javascript string method .substr() combined with the .length property.
var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"
This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string.
You can also use the .slice() method as others have pointed out below.
If you're simply looking to find the characters after the underscore, you could use this:
var tabId = id.split("_").pop(); // => "Tabs1"
This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want).
Answer from Jamon Holmgren on Stack OverflowEDIT: As others have pointed out, use slice(-5) instead of substr. However, see the .split().pop() solution at the bottom of this answer for another approach.
Original answer:
You'll want to use the Javascript string method .substr() combined with the .length property.
var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"
This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string.
You can also use the .slice() method as others have pointed out below.
If you're simply looking to find the characters after the underscore, you could use this:
var tabId = id.split("_").pop(); // => "Tabs1"
This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want).
Don't use the deprecated .substr()!
Use either the .slice() method because it is cross browser compatible (see issue with IE).
Or use the .substring() method.
The are slight differences in requirements, which are properly documented on: String.prototype.substring()
const id = "ctl03_Tabs1";
console.log(id.slice(-5)); //Outputs: Tabs1
console.log(id.slice(-1)); //Outputs: 1
// below is slower
console.log(id.substring(id.length - 5)); //Outputs: Tabs1
console.log(id.substring(id.length - 1)); //Outputs: 1
An elegant and short alternative, is the String.prototype.slice method.
Just by:
Copystr.slice(-1);
A negative start index slices the string from length+index, to length, being index -1, the last character is extracted:
Copy"abc".slice(-1); // "c";
Use charAt:
The charAt() method returns the character at the specified index in a string.
You can use this method in conjunction with the length property of a string to get the last character in that string.
For example:
Copyconst myString = "linto.yahoo.com.";
const stringLength = myString.length; // this will be 16
console.log('lastChar: ', myString.charAt(stringLength - 1)); // this will be the string
Run code snippetEdit code snippet Hide Results Copy to answer Expand