Home >Web Front-end >Front-end Q&A >How to remove the last character from es6 string
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.
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);
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);
You can also use the splice() function
var str = "abcdefg1234567" var arr1=[...str]; console.log(arr1); arr1.splice(-1,1); console.log(arr1);
3. Convert the processed array back to a string
var s =arr1.join(""); //指定分隔符 console.log(s); //返回字符串##[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!