如何获取字符串的最后一个字符:
"linto.yahoo.com."
这个字符串的最后一个字符是“。”
我怎么才能找到这个?
如何获取字符串的最后一个字符:
"linto.yahoo.com."
这个字符串的最后一个字符是“。”
我怎么才能找到这个?
当前回答
使用String.prototype.at()方法是实现它的一种新方法
Const s = "linto.yahoo.com."; Const last = s.at(-1); console.log(去年);
阅读更多关于at的资料
其他回答
试试这个…
const str = "linto.yahoo.com."
console.log(str.charAt(str.length-1));
使用String.prototype.at()方法是实现它的一种新方法
Const s = "linto.yahoo.com."; Const last = s.at(-1); console.log(去年);
阅读更多关于at的资料
您可以使用这个简单的ES6方法
const lastChar = (str) = > str.split (") .reverse () . join(" ")。取代 (',', '')[ str。长度=== str.length + 1 ?1: 0]; / /实例 console.log (lastChar (" linto.yahoo.com "));
这将在所有浏览器中工作。
str.charAt(str.length - 1)
一些浏览器允许(作为一个非标准扩展)你把它缩短为:
str[str.length - 1];
使用JavaScript的charAt函数获取给定0索引位置的字符。使用length来找出字符串的长度。最后一个字符的长度是- 1。 例子:
var word = "linto.yahoo.com.";
var last = word.charAt(word.length - 1);
alert('The last character is:' + last);