📅  最后修改于: 2023-12-03 15:20:21.611000             🧑  作者: Mango
The String.find()
method in JavaScript is used to search for a particular substring within a string and returns the index of the first occurrence of the substring.
str.find(searchValue[, fromIndex])
searchValue
: The substring to search for within the string.fromIndex
(optional): The index to start searching from. If not specified, it defaults to 0.let str = "Hello World!";
console.log(str.find("world")); // returns -1
console.log(str.find("World")); // returns 6
console.log(str.find("o")); // returns 4
console.log(str.find("o", 5)); // returns 7
String.find()
method is case-sensitive. fromIndex
parameter accepts negative values, which count backwards from the end of the string. The String.find()
method in JavaScript is a useful tool for searching strings for substrings. By understanding its syntax, parameters, and return values, programmers can efficiently find substrings within strings for their applications.