字符串查找:
includes(str):判断是否包含参数(str)的字符串startsWith(str):判断是否以参数(str)字符串开头endsWith(str):判断是否以参数(str)字符串结尾
注意事项:
-
这三个方法都支持两个参数,第一个参数要搜索的字符串(必填),第二个参数搜索起始位置索引(可选)。
-
这三个方法都返回true/false,查找字符串的具体位置,还是要用 indexOf 和 lastIndexOf。
-
这三个方法传入了正则表达式而不是字符串,会抛出错误。indexOf 和 lastIndexOf,会将正则表达式转换为字符串并搜索。
let shana = "first,centre,last";
//返回true/false,判断是否找到参数字符串。
shana.includes("first"); // true
//返回true/false,判断参数字符串是否在原字符串的头部。
shana.startsWith("first"); // true
//返回true/false,判断参数字符串是否在原字符串的尾部。
shana.endsWith("last"); // true
shana.startsWith("centre",6); // true
字符串重复:repeat():
repeat(count):重复参数(count)次数
console.log("i".repeat(3)); //iii
console.log("i".repeat(0)); //"" 将原字符串重复0次返回空字符串
//参数是小数,向下取整
console.log("i".repeat(2.3)); //ii
//参数是负数或者Infinity,会报错
console.log("i".repeat(-1)); //RangeError: Invalid count value
console.