I have a string, 12345.00
, and I would like it to return 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. Any suggestions?
To remove the last character from a string in JavaScript, you should use the slice() method. It takes two arguments: the start index and the end index. slice() supports negative indexing, which means that slice(0, -1) is equivalent to slice(0, str. length - 1) .
you can use . indexOf() and . lastIndexOf() to determine if an index is repeated. Meaning, if the first occurrence of the character is also the last occurrence, then you know it doesn't repeat.
You can use the substring function:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With