Home > Web Front-end > JS Tutorial > body text

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

Patricia Arquette
Release: 2024-10-25 13:09:03
Original
733 people have browsed it

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:

<code class="javascript">let str = "12345.00";
str = str.substring(0, str.length - 1);
console.log(str); // Output: 12345.0</code>
Copy after login

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.

The above is the detailed content of How to Remove the Last Character from a String in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!