An elegant and short alternative, is the String.prototype.slice
method.
(String.prototype.slice
方法是一种优雅而简短的选择。)
Just by:
(只是:)
str.slice(-1);
A negative start index slices the string from length+index
, to length
, being index -1
, the last character is extracted:
(负起始索引将字符串从length+index
index切成length
,即index -1
,提取最后一个字符:)
"abc".slice(-1); // "c";
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…