find() 函数用于在字符串中查找指定子字符串的位置。如果找到子字符串,它会返回子字符串在字符串中的起始索引。否则,它会返回 -1。
find(sub_string, start_index, end_index)其中:sub_string:要查找的子字符串。start_index(可选):开始搜索的位置(从 0 开始)。默认为 0。end_index(可选):搜索的结束位置(从 0 开始)。默认为字符串的长度。
const str = "Hello world";// 从字符串开头查找 "world"
const index1 = str.find("world"); // 6// 从字符串索引 5 开始查找 "world"
const index2 = str.find("world", 5); // -1// 从字符串索引 5 到结尾查找 "world"
const index3 = str.find("world", 5, str.length); // 6// 查找不存在的子字符串
const index4 = str.find("not_found"); // -1
本文地址:https://www.qianwe.com/article/c79da68a2d87bf4c33c0.html