Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I chop/slice/trim off last character in string using Javascript?

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?

like image 980
Phill Pafford Avatar asked Jun 04 '09 20:06

Phill Pafford


People also ask

How do you slice a string after a specific character in JavaScript?

Alternatively, you can use the str. split() method. To get the substring after a specific character: Use the split() method to split the string on the character.

How do you trim a character in JavaScript?

JavaScript provides three functions for performing various types of string trimming. The first, trimLeft() , strips characters from the beginning of the string. The second, trimRight() , removes characters from the end of the string. The final function, trim() , removes characters from both ends.


1 Answers

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);
like image 127
Jon Erickson Avatar answered Sep 20 '22 12:09

Jon Erickson