ES6整理笔记

字符串的新增方法

1.includes(),startsWith(),endsWith()
includes():返回布尔值,表示是否找到了参数字符串。
startsWith():返回布尔值,表示参数字符串是否在原字符串的头部
endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部
//传统上,javasrcipt 只有indexOf 方法,可以来确定一个字符串是否包含在另一个字符串中。ES6又提供了三种新方法。后面加个 参数表示开始搜索的位置   startsWith() 从第n=0开始算; endsWith()从前几个字符 n=1   
let hello="Hello world!"'
console.log(hello);
console.log(hello.includes("a"));
console.log(hello.startsWith("w",6));
console.log(hello.endsWith("!"));
console.log(hello.endsWith("w",7));
2. repeat()

repeat()返回一个新字符串,表示将原字符串重复n次。

let hello="Hello world!"
console.log( hello.repeat(12));
3. padStart(),padEnd()

padStart()用于头部补全
padEnd()用于尾部补全
//参数字符串,n表示补完一共多少个数 n=1开始算

let x= "xx";
console.log(x.padStart(4,"ab"))
console.log(x.padStart(3,"ab"))
console.log(x.padStart(7,"ab"))
console.log(x.padEnd(4,"ab"))
console.log(x.padEnd(9,"ab"))
4. trim(),trimStart(),trimEnd()

trim()消除字符串所有空格
trimStart()消除字符串头部的空格
trimEnd()消除尾部的空格

const s="  abc    "
console.log(s)
console.log(s.trim())
console.log(s.trimStart())
console.log(s.trimEnd())
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值