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
Run code snippetEdit code snippet Hide Results Copy to answer Expand
jquery - How can I select the last 4 characters of a string using JavaScript? - Stack Overflow
Get last 4 characters of every element of array in JavaScript - Stack Overflow
swift - How to get last 4 characters of a string? - Stack Overflow
Nth-to-Last Character in a String
If I understand your question correctly (and I'm not sure I do), you just want to take the last 4 characters of a known string (to translate into decimal, but that wasn't your question).
In javascript, you would want to use the substring api. For example, to get the last 4 digits of an arbitrary string, you could do this:
var str = "abcdefghijklmnop";
var substr = str.substring(str.length-4, str.length);
More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
Use the String.substr(startPosition, numChars) method:
var myString = "abcdefg";
console.log(myString.substr(-4, 4)); // Start 4 chars before end and take 4 chars
['11114444', '22224444'].map(function(string) { return string.substring(string.length - 4, string.length) })
You can use Array.map depending on your targeted browser support/polyfills. And you can do the following:
for( var i = 0; i < array.length; ++ i ) {
array[i] = array[i].substr(-4);
}
Swift 2:
A solution is substringFromIndex
let a = "StackOverFlow"
let last4 = a.substringFromIndex(a.endIndex.advancedBy(-4))
or suffix on characters
let last4 = String(a.characters.suffix(4))
Swift 3:
In Swift 3 the syntax for the first solution has been changed to
let last4 = a.substring(from:a.index(a.endIndex, offsetBy: -4))
Swift 4+:
In Swift 4 it becomes more convenient:
let last4 = a.suffix(4)
The type of the result is a new type Substring which behaves as a String in many cases. However if the substring is supposed to leave the scope where it's created in you have to create a new String instance.
let last4 = String(a.suffix(4))
String substr = a.substring(a.length() - 4)
syntax is wrong. no type before vars in Swift.
let a = "1234567890"
let last4 = String(a.characters.suffix(4))
print(last4)
works on Swift 3.0