ES6字符串扩展方法

字符串扩展方法

let str="abcdef";

1.字符的 Unicode 表示法
	"\u4e00",表示中文“一”。
2.includes()
	表示是否找到了参数字符串
    console.log(str.includes("d"));//true
3.startsWith()
	表示参数字符串是否在原字符串的头部
    (1)console.log(str.startsWith("a"));//true
        判断字符a是否在最前面
    (2)console.log(str.startsWith("b",1));//true
        从1号位开始,判断字符b是否在最前面
4.endsWith()
	表示参数字符串是否在原字符串的尾部
    (1)console.log(str.endsWith("ef"));//true
        判断字符ef是否在最后面
    (2)console.log(str.endsWith("de",5));//true
        从5号位往前,判断字符de是否在最后面
5.repeat()
	表示将原字符串重复几次,返回新的字符串
    str=str.repeat(3);//重复字符串
6.padStart()
    console.log("#"+Math.floor(Math.random()*0x1000000).toString(16).padStart(6,"0"));
	padStart(6,"0")
	如果不够指定长度,在前面补足长度,判断是否够6位,不足在前面补"0"
7.padEnd()
	在后面补齐字符串
8.模板字符串
	let n=3;
    console.log("a"+n+"b")
    这种写法很不方便,我们使用下面的写法
    let n=3;
    console.log(`a${n}b`)
    注意这里的字符串使用``将里面引住
    也可以用于:console.log `你好`;省略了括号
9.fill()
    fill(值,从什么位置开始,到什么位置结束),必须数组有长度,而且填充会覆盖
    (1)var arr=[];
        arr.length=10;
        arr.fill(3);
        console.log(arr);
    (2)var arr=new Array(5).fill(10);
    (3)arr.fill(5,3,6);
10.flatMap扁平化数组(ES8以后才有)
    var arr=[[1,2,3],0,[4,5,6],[7,8,9]];
    var arr1=arr.flatMap(function(item){
        return item
    });
    console.log(arr1);//1230456789
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值