日常打卡,今日學習呀學習....
怎么說呢,ES6乃至JS,學習之路漫長呀呀呀.
- 字符串的擴展
includes('str')
:判斷指定位置有沒有字符,返回值是布爾值
let ss = 'asd';
console.log(ss.includes('a'));//true
console.log(ss.includes('e'));//false
startsWith('str',index)
:判斷字符串是不是以指定字符作為開頭,index表示查找的結束下標`
let str = 'aasderst';
console.log(str.startsWith('s',2)); //true
console.log(str.startsWith('s',3));//false
console.log(str.startsWith('a'));//true
endsWith('str',num)
:判斷字符串是不是以指定字符作為結尾,num表示查找的個數
let s3 = 'weasfer';
console.log(s3.endsWith('r')); //true
console.log(s3.endsWith('a',3));//true
console.log(s3.endsWith('a',2));//false
repeat(num)
:將字符串重復幾次,num不可以是負數和Infitnity
let s4 = 'asd';
console.log(s4.repeat(2));//asdasd
console.log(s4.repeat(0));console.log(s4.repeat(-0)); //空串
console.log(s4.repeat(0.9));console.log(s4.repeat(-0.5));//空串
padStart('指定字符串長度',補全的字符串)
:在頭部補全字符串
let s5 = 'llo';
console.log(s5.padStart(2,'he'));//llo
console.log(s5.padStart(3,'he'));//llo
console.log(s5.padStart(5,'he'));//hello
console.log(s5.padStart(7,'he'));//hehello
console.log(s5.padStart(8,'he'));//hehehllo
padEnd('指定字符串長度',補全的字符串)
:在末尾部補全字符串
let s6 = 'llo';
console.log(s6.padEnd(2,'ed'));//llo
console.log(s6.padEnd(3,'ed'));//llo
console.log(s6.padEnd(5,'ed'));//lloed
console.log(s6.padEnd(7,'ed'));//lloeded
console.log(s6.padEnd(8,'ed'));//lloedede
注意:原字符串的長度大于或者等于指定的最小字符串長度,返回原字符串