Monday, September 24, 2018

JavaScript chop/slice/trim off last character in string



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?


Answer



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




No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...