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

在es6中,可以利用“Array.isArray()”方法判断对象是否为数组,若判断的对象是数组,返回的结果是true,若判断对象不是数组,返回的结果是false,语法为“Array.isArray(需要检测的js对象)”。

在es6中,可以通过replace()方法去掉字符串的所有空格,只需要使用replace()方法配合正则表达式“/\s/g”来查找字符串中的全部空格,并将其替换为空字符即可;去除语法为“字符串对象.replace(/\s/g,"")”。

在es6中,可用Object对象的is()方法来判断两个对象是否相等,该方法检测两个变量的值是否为同一个值,判断两个对象的引用地址是否一致,语法“Object.is(对象1,对象2)”;该方法会返回布尔值,若返回true则表示两个对象相等。

转换方法:1、利用“+”给数字拼接一个空字符,语法“数字+""”;2、使用String(),可把对象的值转换为字符串,语法“String(数字对象)”;3、用toString(),可返回数字的字符串表示,语法“数字.toString()”。

sort排序是es6中的;sort排序是es6中用于对数组的元素进行排序的方法,该方法默认不传参,按照字符编码顺序进行排序,排序顺序可以是字母或数字,并按升序或降序,语法为“array.sort(callback(a,b))”。

改变方法:1、利用splice()方法修改,该方法可以直接修改原数组的内容,语法为“数组.splice(开始位置,修改个数,修改后的值)”;2、利用下标访问数组元素,并重新赋值来修改数组数据,语法为“数组[下标值]=修改后的值;”。

在es6中,import as用于将若干export导出的内容组合成一个对象返回;ES6的模块化分为导出与导入两个模块,该方法能够将所有的导出内容包裹到指定对象中,语法为“import * as 对象 from ...”。

ie8不支持es6。在ie浏览器中,ie10及以下版本是完全不支持es6的,解析不了es6的语法,而ie11可以支持部分es6特性;可以通过判断ie浏览器是否支持箭头函数来检查它是否支持es6,当不支持箭头函数,则代表浏览器不支持ES6。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.