1. 字符串首尾補全長度 padStart()、padEnd()
str.padStart(len, '要補的字符串') // 夠len位不補全,不夠首端補字符串
str.padEnd(len, '要補的字符串')
'2'.padStart(2,'0') // '02'
'21'.padStart(2,'0') // '21'
'2'.padEnd(2,'#') //'2#'
2. 檢測字符串是否以指定的子字符串開始 startsWith()
str.startsWith(searchvalue,start)
- searchvalue:必需,要查找的字符串。start:可選,從索引為幾開始查找
- 返回值:true/false
- 注意:該方法對大小寫敏感
"Hello world, welcome to the Runoob.".startsWith('Hello') //true
"Hello world, welcome to the Runoob.".startsWith('hello') //false
"Hello world, welcome to the Runoob.".startsWith('world',6) //true