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

javascript delete last character of string

青灯夜游
青灯夜游Original
2021-04-09 17:45:3722955browse

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,”;

Method 1:

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

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

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

Method 3:

basic = basic.substring(0, basic.lastIndexOf(','));

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!

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