"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Remove the Last Character from a String in JavaScript?

How to Remove the Last Character from a String in JavaScript?

Published on 2024-11-06
Browse:616

How to Remove the Last Character from a String in JavaScript?

Trimming the Last Character from a String in JavaScript

You have a string, "12345.00," and you want it to return "12345.0." While trim would remove whitespace, you can consider the substring function for this task.

Solution Using substring

The substring function allows you to extract a substring from a specified start and end index. To eliminate the last character, you can specify the start index as 0 and the end index as the original string's length minus 1. Here's an example:

let str = "12345.00";
str = str.substring(0, str.length - 1);
console.log(str); // Output: 12345.0

This code creates a new string by extracting characters from index 0 (the beginning) to the index before the last character (length - 1). It effectively chops off the last character and assigns the resulting string back to the original variable.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3