Home >Web Front-end >Front-end Q&A >How to remove the last character from es6 string

How to remove the last character from es6 string

青灯夜游
青灯夜游Original
2022-05-05 15:05:514038browse

Method: 1. Use "[...str]" or "Array.from(str)" to convert the string into a character array; 2. Use "arr.pop()" or "arr. splice(-1,1)" to delete the last element of the array; 3. Use "arr.join("")" to convert the processed array back to a string.

How to remove the last character from es6 string

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

Remove the last character from an es6 string

In es6, if you want to remove the last character from a string, you can use an array

1. First use the spread operator "..." or Array.from() to convert the string into a character array

var str = "abcdefg1234567"
var arr1=[...str];
var arr2=Array.from(str);
console.log(arr1);
console.log(arr2);

How to remove the last character from es6 string

2. Delete the last element of the character array

You can use the pop() function

var str = "abcdefg1234567"
var arr1=[...str];
console.log(arr1);
console.log(arr1.pop());
console.log(arr1);

How to remove the last character from es6 string

You can also use the splice() function

var str = "abcdefg1234567"
var arr1=[...str];
console.log(arr1);
arr1.splice(-1,1);
console.log(arr1);

How to remove the last character from es6 string

3. Convert the processed array back to a string

var s =arr1.join("");  //指定分隔符
console.log(s);  //返回字符串

How to remove the last character from es6 string

##[Related Recommended:

javascript video tutorial, web front-end

The above is the detailed content of How to remove the last character from es6 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