<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>indexOf</title> </head> <body> <script> let str = "你好,我好,大家好!"; // "好"字符首次出现的下标。 let i = str.indexOf("好"); console.log(i); // 1 // 从下标2开始查找,"好"字符 首次出现的下标。 i = str.indexOf("好",2); console.log(i); // 4 // 从前往后找,到下标5为止,"好"字符,最后一次出现的下标。 i = str.lastIndexOf("好",5); console.log(i); // 4 // "好"字符,最后一次出现的下标。 i = str.lastIndexOf("好"); console.log(i); // 8 // 如果没找到对应的字符函数返回-1。 i = str.lastIndexOf("它"); console.log(i); // -1 console.log(str.length); // 10 console.log(str[str.length - 1]); // ! console.log(str[str.length]); // undefined console.log(typeof str[str.length]); // undefined console.log(typeof str[str.length] == "undefined"); // true console.log(typeof str[str.length] === "undefined"); // true console.log(str, "的长度是,", str.length); // 你好,我好,大家好! 的长度是, 10 console.log(str+" 的字符数为"+str.length); // 你好,我好,大家好! 的字符数为10 </script> </body> </html>
js str indexof
猜你喜欢
转载自blog.csdn.net/weixin_42193179/article/details/85678169
今日推荐
周排行