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

青灯夜游
Release: 2022-05-05 18:47:12
Original
4042 people have browsed it

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);
Copy after login

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);
Copy after login

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);
Copy after login

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);  //返回字符串
Copy after login

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!

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