1. indexOf() 检测是否包含某字符
返回值:包含返回5 不包含返回-1
let str = "helloAAAwordeddd";
let res = str.indexOf("sss");
console.log(res); //没有返回-1
let res1 = str.indexOf("wor");
console.log(res1); //有返回5
2.includes(搜索的字符串,[检测位置索引号]);
返回值:有返回true 没有返回false
let str = "helloAAAwordeddd";
let res3 = str.includes("ddd");
console.log(res3); //true
3.startsWith(检测字符串,[检测位置索引号]); 检测是否以某字符开头
返回值:是返回true 否返回false
let str = "helloAAAwordeddd";
let res5 = str.startsWith("a", 0);
console.log(res5); //false
let res6 = str.startsWith("h", 0);
console.log(res6); //true
4.endsWith(检测字符串,[检测位置索引号]); 检测是否以某字符结尾
返回值:是返回true 否返回false
let str = "helloAAAwordeddd";
let res10 = str.endsWith("a", 0);
console.log(res10); //false
let res11 = str.endsWith("d", [str.length - 1]);
console.log(res11); //true
5.trim() 去除两端空格
返回值:去除空格后的字符串
let str2 = " hellowordeddd ";
let res7 = str2.trim();
console.log(res7);
6.toUpperCase() 用于将字母转为大写
返回值:返回转换之后的新字符串
let str = "helloAAAwordeddd";
let res8 = str.toUpperCase("d");
console.log(res8);//HELLOAAAWORDEDDD
7.toLowerCase() 用于将字母转为小写
返回值:返回转换之后的新字符串
let str = "helloAAAwordeddd";
let res9 = str.toLowerCase("A");
console.log(res9);//helloaaawordeddd
8.split('分隔符') 用来将字符串拆分数组
默认为逗号分隔
返回值: 返回一个分割好的新数组
let str = "helloAAAwordeddd";
let res2 = str.split("");
console.log(res2);//['h', 'e', 'l', 'l', 'o', 'A', 'A', 'A', 'w', 'o', 'r', 'd',
'e', 'd', 'd', 'd']
9.substring(需要截取的第一个字符索引号,[结束的索引号]);
[star,end)包前不包后
返回值:返回截取的字符串
let str = "helloAAAwordeddd";
let res4 = str.substring(1, 5);
console.log(res4); //ello
10.substr(开始索引,多少个);截取字符串
返回值:截取出来的字符串
let str = "helloAAAwordeddd";
var res12 = str.substr(0, 5);
console.log(res12); //hello
11.replace() 替换字符串中的某个字符
返回值 替换好的字符串
var str3 = 'aaabbbgdbdeeeeeef';
var res13 = str3.replace('g', 'k');
console.log(res13);//aaabbbkdbdeeeeeef
12.charAt(索引) 查找索引号对应的字符串
返回值:该索引位置对应的字符
let str = "helloAAAwordeddd";
var res14 = str.charAt(5);
console.log(res14); //A