Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

javascript - 如何获取字符串的最后一个字符?(how to get the last character of a string?)

How to get the last character of the string:

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

"linto.yahoo.com."

The last character of this string is "."

(该字符串的最后一个字符是".")

How can I find this?

(我怎么找到这个?)

  ask by Linto P D translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...