1.寻找方法
方法 | 返回值 | 用法 |
---|---|---|
indexOf (最开始出现的位置,从前往后找) | 返回下标,没有返回-1 | array.indexOf(item,start) |
lastIndexOf (最后出现的位置,从后往前找) | 返回下标,没有返回-1 | array.lastIndexOf(item,start) |
includes | 包含返回true,不包含返回false | str.includes(“world”); |
charAt() | 在指定位置的字符 | str.charAt(2) |
charCodeAt() | 返回字符串第一个字符的 Unicode 编码(H 的 Unicode 值) | str.charCodeAt(0); |
startsWith | 是以一个字符串开头返回true,不是返回falsee | str.startsWith(“world”, 6); |
2.转换方法
方法 | 返回值 | 用法 |
---|---|---|
split | 返回一个字符串数组 | str.split(" ",3); |
toString | 返回一个字符串 | fruits.toString() |
valueOf(不会显示调用) | 数组本身 | arr.valueOf() |
toLowerCase | 返回小写的字符串 | str.toLowerCase() |
toUpperCase | 返回大写的字符串 | str.toUpperCase() |
trim | 删除头尾空格的字符串 | str.trim() |
3.操作方法
方法 | 返回值 | 用法 |
---|---|---|
concat | 返回连接的字符串 | str.concat(str2) |
repeat | 返回复制指定次数并连接在一起的字符串。 | string.repeat(count) |
slice( 截取字符串) | 返回一个新字符串,从start到end前面 | *str.slice(start, end) |
substr | 返回截取的字符串 | string.substr(start,length) |
substring( 到to之前的) | 返回截取的字符串 | string.substring(from, to) |
4.正则方法
方法 | 返回值 | 用法 |
---|---|---|
match | 匹配到的对象的数组 | str.match(/ain/g) |
replace | 一个新的字符串 | str.replace(“Microsoft”,“Runoob”); |
search | 有返回下标,没有返回-1 | str.search(/blue/i) |