How to select last two characters of a string
JavascriptStringJavascript Problem Overview
I need to select last two characters from the variable, whether it is digit or letters.
For example:
var member = "my name is Mate";
I would like to show last two letters from the string in the member
variable.
Javascript Solutions
Solution 1 - Javascript
You can pass a negative index to .slice()
. That will indicate an offset from the end of the set.
var member = "my name is Mate";
var last2 = member.slice(-2);
alert(last2); // "te"
Solution 2 - Javascript
EDIT: 2020: use string.slice(-2)
as others say - see below.
now 2016 just string.substr(-2)
should do the trick (not substring(!))
taken from MDN
> Syntax
>
> str.substr(start[, length])
>
> Parameters
>
> start
>
> Location at which to begin extracting characters. If a negative number is given, it is treated as strLength + start where strLength is
> the length of the string (for example, if start is -3 it is treated as
> strLength - 3.) length
> Optional. The number of characters to extract.
EDIT 2020
MDN says
> Warning: Although String.prototype.substr(…) is not strictly > deprecated (as in "removed from the Web standards"), it is considered > a legacy function and should be avoided when possible. It is not part > of the core JavaScript language and may be removed in the future.
Solution 3 - Javascript
Try this, note that you don't need to specify the end index in substring
.
var characters = member.substr(member.length -2);
Solution 4 - Javascript
The following example uses slice()
with negative indexes
var str = 'my name is maanu.';
console.log(str.slice(-3)); // returns 'nu.' last two
console.log(str.slice(3, -7)); // returns 'name is'
console.log(str.slice(0, -1)); // returns 'my name is maanu'
Solution 5 - Javascript
You can try
member.substr(member.length-2);
Solution 6 - Javascript
If it's an integer you need a part of....
var result = number.toString().slice(-2);
Solution 7 - Javascript
You should use substring, not jQuery, to do this.
Try something like this:
member.substring(member.length - 2, member.length)
W3Schools (not official, but occasionally helpful): http://www.w3schools.com/jsref/jsref_substring.asp
Adding MDN link as requested by commenter: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring
Solution 8 - Javascript
Shortest:
str.slice(-2)
Example:
const str = "test";
const last2 = str.slice(-2);
console.log(last2);
Solution 9 - Javascript
var member = "my name is maanu";
var answer=member.substring(0,member.length - 2);
alert(answer);
Solution 10 - Javascript
Slice can be used to find the substring. When we know the indexes we can use an alternative solution like index wise adder. Both are taking roughly the same time for execution.
const primitiveStringMember = "my name is Mate";
const objectStringMember = new String("my name is Mate");
console.log(typeof primitiveStringMember);//string
console.log(typeof objectStringMember);//object
/* However when we use . operator to string primitive type, JS will wrap up the string with object. That's why we can use the methods String object type for the primitive type string.
*/
//Slice method
const t0 = performance.now();
slicedString = primitiveStringMember.slice(-2);//te
const t1 = performance.now();
console.log(`Call to do slice took ${t1 - t0} milliseconds.`);
//index vise adder method
const t2 = performance.now();
length = primitiveStringMember.length
neededString = primitiveStringMember[length-2]+primitiveStringMember[length-1];//te
const t3 = performance.now();
console.log(`Call to do index adder took ${t3 - t2} milliseconds.`);