admin管理员组

文章数量:1131371

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.

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.

Share Improve this question edited Jun 26, 2020 at 12:48 Mo. asked May 24, 2012 at 16:50 Mo.Mo. 27.4k36 gold badges166 silver badges232 bronze badges
Add a comment  | 

10 Answers 10

Reset to default 652

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"

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.

Try this, note that you don't need to specify the end index in substring.

var characters = member.substr(member.length -2);

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'

You can try

member.substr(member.length-2);

If it's an integer you need a part of....

var result = number.toString().slice(-2);

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

var member = "my name is maanu";

var answer=member.substring(0,member.length - 2);

alert(answer);

Shortest:

str.slice(-2)

Example:

const str = "test";
const last2 = str.slice(-2);

console.log(last2);

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.`);

本文标签: javascriptHow to select last two characters of a stringStack Overflow