字符串扩展方法
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