Home  >  Article  >  Web Front-end  >  How to Remove the Last Character from a String in JavaScript?

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

Patricia Arquette
Patricia ArquetteOriginal
2024-10-25 13:09:03734browse

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>

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!

Statement:
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