<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js_str.indexOf().lastIndexOf()</title>
</head>
<body>
<script>
/*参考:
https://www.runoob.com/jsref/jsref-indexof.html
https://www.runoob.com/jsref/jsref-lastindexof.html
* 知识点:
1.string.indexOf(searchValue,start) -> searchValue 第一次出现的位置 / 没找到匹配的字符串,则返回 -1 (number)。
从前往后。
searchValue,必需。待检索的字符串。
start,可选的整数。规定在字符串中开始检索的位置。
它的合法取值是 0 到 string Object.length - 1。
如省略该参数,则将从字符串的首字符开始检索。
2.string.lastIndexOf(searchValue,start) -> searchValue 最后出现的位置 / 没找到匹配的字符串,则返回 -1。
从后往前。
searchValue,必需。待检索的字符串。
start,可选的整数。规定在字符串中开始检索的位置。
它的合法取值是 0 到 stringObject.length - 1。
如省略该参数,则将从字符串的最后一个字符处开始检索。
* */
// 1.string.indexOf()
let str = "你好,我好,大家好!";
let i = str.indexOf("好");
console.log(i); // 1
i = str.indexOf("好", 2);
console.log(i); // 4
console.log(str.indexOf("们")); // -1
// 2.string.lastIndexOf()
console.log("\n2.");
i = str.lastIndexOf("好", 5);
console.log(i); // 4
i = str.lastIndexOf("好");
console.log(i); // 8
i = str.lastIndexOf("它");
console.log(i); // -1
// 3.
console.log("\n3.");
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(str[str.length] == "undefined"); // false
console.log(typeof str[str.length] === "undefined"); // true
</script>
</body>
</html>
js_str.indexOf().lastIndexOf().html
最新推荐文章于 2024-01-03 09:15:00 发布