js常用方法

2024-03-05  本文已影响0人  85d8c4f3886f

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)

"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
上一篇 下一篇

猜你喜欢

热点阅读