How to remove the last character from an es6 string
This article mainly introduces the es6 string how to remove the last character of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that after reading this es6 string how to remove the last character of the article will have a harvest, let's take a look.
Methods: 1, use "[... str]" or "Array.from (str)" to convert the string into a character array; 2, delete the last element of the array with "arr.pop ()" or "arr.splice (- 1 str)"; 3, use "arr.join (") "to convert the processed array back to the string.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
Es6 string removes the last character
In es6, to remove the last character of a string, you can use an array
1. Use the extension operator "." Or Array.from () converts a string to 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 (- 1Magne1); console.log (arr1)
3. Convert the processed array back to the string
Var s = arr1.join (""); / / specify the delimiter console.log (s); / / return the string
This is the end of the article on "how to remove the last character from an es6 string". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to remove the last character from the es6 string". If you want to learn more, you are welcome to follow the industry information channel.