如何获取字符串的最后一个字符:

"linto.yahoo.com."

这个字符串的最后一个字符是“。”

我怎么才能找到这个?


当前回答

str.charAt(str.length - 1)

一些浏览器允许(作为一个非标准扩展)你把它缩短为:

str[str.length - 1];

其他回答

var string = "Hello"; Var fg = string.length; Fg = Fg - 1; console.log (string (fg));

使用参数为-1的substr:

“linto.yahoo.com”。.substr (1);

=“。”

注意:

要从字符串的末尾提取字符,请使用负的起始数字(这在ie8及更早版本中不起作用)。

str.charAt(str.length - 1)

一些浏览器允许(作为一个非标准扩展)你把它缩短为:

str[str.length - 1];

使用charAt:

charAt()方法的作用是:返回字符串中指定下标处的字符。

您可以将此方法与字符串的length属性结合使用,以获取该字符串中的最后一个字符。 例如:

const myString = "linto.yahoo.com."; const stringLength = myString.length;//这将是16 console.log('lastChar: ', myString. log)charAt(stringLength - 1));//这将是字符串

使用String.prototype.at()方法是实现它的一种新方法

Const s = "linto.yahoo.com."; Const last = s.at(-1); console.log(去年);

阅读更多关于at的资料