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
356 views
in Technique[技术] by (71.8m points)

javascript - JavaScript截断/切片/修剪掉字符串中的最后一个字符(JavaScript chop/slice/trim off last character in string)

I have a string, 12345.00 , and I would like it to return 12345.0 .

(我有一个字符串12345.00 ,我希望它返回12345.0 。)

I have looked at trim , but it looks like it is only trimming whitespace and slice which I don't see how this would work.

(我已经看过trim ,但是看起来它只是在修剪空白和slice ,我看不到它如何工作。)

Any suggestions?

(有什么建议么?)

  ask by Phill Pafford translate from so

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

1 Reply

0 votes
by (71.8m points)

You can use the substring function:

(您可以使用substring函数:)

 let str = "12345.00"; str = str.substring(0, str.length - 1); console.log(str); 

This is the accepted answer, but as per the conversations below, the slice syntax is much clearer:

(这是公认的答案,但是根据下面的对话, 切片语法更加清晰:)

 let str = "12345.00"; str = str.slice(0, -1); console.log(str); 


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

...