4-字符串扩展与新增方法

1、charAt()与at()是什么作用?

答:都接受一个位置参数,返回该位置的字符。只不过at()的搜索范围更大一些,可以识别Unicode大于0xffff的字符。

let str = 'abc';
let res = str.charAt(0)
console.log(res) //a

2、判断一个字符串是否包含在字符串里的方法有哪些?

答:一共有四种:inculdes()/startWith()/endsWith()/indexOf()

前三个方法返回的是布尔值,分别表示 字符中是否包含该字符串 、 字符串是否以该字符作为起始位置 、 字符串是否以该字符作为结束位置。

indexOf方法返回的是该字符在字符串中的位置,位置从0开始。

let str = 'abcde';
let res1 = str.indexOf('bc')
let res2 = str.includes('bc')
let res3 = str.startsWith('bc')
let res4 = str.endsWith('bc')
console.log(res1, res2, res3, res4) //1 true false false

3、repeat()的用法?

答:接受一个数字参数,表示该字符串要重复的次数,并返回重复后的字符串。

let str = 'abc'
let res = str.repeat(3)
console.log(res) //abcabcabc

4、padStart()和padEnd()的用法?

答:接受两个参数,字符串的长度和进行补全的字符串。如果某个字符串的长度不够指定的长度,会在头部或尾部进行补全。

let str = 'abc'
let res = str.padStart(5, '12')
let str2 = 'abc'
let res2 = str.padStart(2, '12')
console.log(res, res2) //12abc abc

三、字符串位置方法

indexOf

lastIndexOf

四、trim()方法

trim()

trimLeft()

trimRight()

五、大小写转换方法

六、模式匹配方法

match():只接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。它返回一个数组。

search():接受参数与match相同。返回的是第一个匹配项的索引,如果没有,返回-1.

replace():

split():join的反义。

七、localeCompare()方法

八、fromCharCode()方法

猜你喜欢

转载自www.cnblogs.com/qingshanyici/p/10868167.html