如何选择字符串的最后两个字符

时间:2023-01-04 17:06:56

I need to select last two characters from the variable, whether it is digit or letters.

我需要从变量中选择最后两个字符,无论是数字还是字母。

For example:

例如:

var member = "my name is maanu";

I would like to show last two letters from the string in the member variable.

我想在成员变量中显示字符串的最后两个字母。

8 个解决方案

#1


258  

You can pass a negative index to .slice().

可以将负索引传递给.slice()。

var member = "my name is maanu";

var last2 = member.slice(-2);

alert(last2); // "nu"

#2


18  

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

尝试一下,注意您不需要在子字符串中指定结束索引。

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

#3


15  

now 2016 just string.substr(-2) should do the trick (not substring(!))

现在,2016年只是string.substr(-2)应该完成了(不是substring(!))

taken from MDN

取自MDN

Syntax

语法

str.substr(start[, length])

str.substr(启动[,])

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.

开始提取字符的位置。如果给定一个负数,则将其视为strLength + start,其中strLength是字符串的长度(例如,如果start是-3,则将其视为strLength -3 .)长度可选。要提取的字符数。

#4


5  

You can try

你可以试着

member.substr(member.length-2);

#5


2  

var member = "my name is maanu";

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

var = member.substring(0,成员回答。长度- 2);

alert(answer);

#6


1  

You should use substring, not jQuery, to do this.

为此,应该使用子字符串,而不是jQuery。

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

W3Schools(不是官方的,但偶尔有用):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

按照评论者的要求添加MDN链接:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring

#7


1  

The following example uses slice() with negative indexes

下面的示例使用带负索引的slice()

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'

#8


0  

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

如果你需要一个整数的一部分....

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

#1


258  

You can pass a negative index to .slice().

可以将负索引传递给.slice()。

var member = "my name is maanu";

var last2 = member.slice(-2);

alert(last2); // "nu"

#2


18  

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

尝试一下,注意您不需要在子字符串中指定结束索引。

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

#3


15  

now 2016 just string.substr(-2) should do the trick (not substring(!))

现在,2016年只是string.substr(-2)应该完成了(不是substring(!))

taken from MDN

取自MDN

Syntax

语法

str.substr(start[, length])

str.substr(启动[,])

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.

开始提取字符的位置。如果给定一个负数,则将其视为strLength + start,其中strLength是字符串的长度(例如,如果start是-3,则将其视为strLength -3 .)长度可选。要提取的字符数。

#4


5  

You can try

你可以试着

member.substr(member.length-2);

#5


2  

var member = "my name is maanu";

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

var = member.substring(0,成员回答。长度- 2);

alert(answer);

#6


1  

You should use substring, not jQuery, to do this.

为此,应该使用子字符串,而不是jQuery。

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

W3Schools(不是官方的,但偶尔有用):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

按照评论者的要求添加MDN链接:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring

#7


1  

The following example uses slice() with negative indexes

下面的示例使用带负索引的slice()

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'

#8


0  

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

如果你需要一个整数的一部分....

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