JavaScript切断/切片/修剪字符串中的最后一个字符 [英] JavaScript chop/slice/trim off last character in string
本文介绍了JavaScript切断/切片/修剪字符串中的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串, 12345.00
,我希望它返回 12345.0
。
I have a string, 12345.00
, and I would like it to return 12345.0
.
我看过 trim
,但看起来它只是修剪空白和切片
我不明白这是怎么回事。有什么建议吗?
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. Any suggestions?
推荐答案
你可以使用 substring function:
You can use the substring function:
var str = "12345.00";
str = str.substring(0, str.length - 1); // "12345.0"
这是接受的答案,但根据下面的对话, slice 语法更加清晰:
This is the accepted answer, but as per the conversations below, the slice syntax is much clearer:
var str = "12345.00";
str = str.slice(0, -1); // "12345.0"
这篇关于JavaScript切断/切片/修剪字符串中的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文