Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to remove characters from end of string? [duplicate]

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 532
Phill Pafford Avatar asked Jun 04 '09 20:06

Phill Pafford


People also ask

How do I remove a character from the end of a string in JavaScript?

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

How do you find duplicate characters in a given string in JavaScript?

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.


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 147
Jon Erickson Avatar answered Oct 14 '22 21:10

Jon Erickson