字符串常用方法
字符串也基本可以归纳为增删改查
增
concat(): 用于将一个或多个字符串拼接成一个新字符串
删
slice() substr() substring() 这三个方法都返回调用它们的字符串的一个子字符串,而且都接收一或两个参数。
js
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"
console.log(stringValue.substring(3,7)); // "lo w"
console.log(stringValue.substr(3, 7)); // "lo worl"改
trim()、trimLeft()、trimRight(): 删除前、后或前后所有空格符,再返回新的字符串
repeat():接收一个整数参数,表示要将字符串复制多少次,然后返回拼接所有副本后的结果
toLowerCase()、 toUpperCase()
查
chatAt()
indexOf()
startWith()
includes()
字符串转为数组
split():把字符串按照指定的分割符,拆分成数组中的每一项
正则模板匹配
match():接收一个参数,可以是一个正则表达式字符串,也可以是一个RegExp对象,返回数组
search():接收一个参数,可以是一个正则表达式字符串,也可以是一个RegExp对象,找到则返回匹配索引,否则返回 -1
replace():接收两个参数,第一个参数为匹配的内容,第二个参数为替换的元素(可用函数)