Home > Article > Web Front-end > javascript delete last character of string
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'))”.
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!