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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10741899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:44:21  来源:igfitidea点击:

How to select last two characters of a string

javascriptstring

提问by Mo.

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 membervariable.

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

回答by

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

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

var member = "my name is maanu";

var last2 = member.slice(-2);

alert(last2); // "nu"

回答by mattytommo

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

试试这个,注意你不需要在substring.

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

回答by halfbit

EDIT:?2020: use string.slice(-2)as others say - see below.

编辑:?2020 年:string.slice(-2)按照其他人的说法使用- 见下文。

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

现在 2016string.substr(-2)应该可以解决问题(不是子字符串(!))

taken from MDN

摘自 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 + startwhere 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.

句法

str.substr(start[, length])

参数

开始

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

EDIT?2020

编辑?2020

MDN says

MDN 说

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.

警告:虽然 String.prototype.substr(...) 并未严格弃用(如“从 Web 标准中删除”),但它被视为遗留函数,应尽可能避免使用。它不是核心 JavaScript 语言的一部分,将来可能会被删除。

回答by Shrroy

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'

回答by thecodeparadox

You can try

你可以试试

member.substr(member.length-2);

回答by Harijs Krūtainis

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

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

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

回答by Matt

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

回答by Biju s

var member = "my name is maanu";

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

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

alert(answer);