Home > Web Front-end > JS Tutorial > javascript delete last character of string

javascript delete last character of string

青灯夜游
Release: 2023-01-04 09:35:58
Original
22932 people have browsed it

Delete method: 1. Use substr(), the syntax is "substr(0, string length-1);"; 2. Use substring(), the syntax is "substring(0, string length-1) ” or “substring(0,str.lastIndexOf('character'))”.

javascript delete last character of string

The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.

js three ways to delete the last character of a string

For example, the string is as follows

var basic = “abc,def,ghi,”;
Copy after login

Method 1:

basic = basic.substr(0, basic.length - 1);
Copy after login

substr() method can extract the specified number of characters starting from the start subscript in the string.

Method 2:

basic = basic.substring(0, basic.length - 1);
Copy after login

substring() method is used to extract the characters between two specified subscripts in the string.

Method 3:

basic = basic.substring(0, basic.lastIndexOf(','));
Copy after login

lastIndexOf() method can return the position where a specified string value last appears, from the specified position in a string backwards Search before.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of javascript delete last character of string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template