We will explain how to search for a specific string in JavaScript .
How can I search for a specific string?
You can search for strings using methods such as
indexOf and
lastIndexOf .
indexOf and lastIndexOf to search for a specific string
To search for a string, use the indexOf method and lastIndexOf method of the String object.
The indexOf method searches from the beginning of the string and returns the found position.
The second argument can specify the position to start the search.
string.indexOf(search string [, search start position ]);
The lastIndexOf method searches from the end of the string and returns the found position.
The second argument can specify the position to start the search.
String.lastIndexOf(search string [, search start position ]);
The example below is a sample that searches for “fun” from the string “JavaScript is fun”.
let str = "JavaScript is fun, fun, fun";
let position = str.indexOf("fun", 16);
console.log(position); // 21
let str = "JavaScript is fun, fun, fun";
let position = str.lastIndexOf("fun", 16);
console.log(position); // 15
“includes”, “startsWith”, “endsWith” to determine if a specific string is included
There are methods to search for strings such as “includes”, “startsWith”, and “endsWith”.
There are methods like the following to search for a specific string.
method name | Explanation |
---|---|
includes(str [,position]) | Determine whether the specified string is included |
startsWith(str [,position]) | Determine whether a string starts with a specified string |
endsWith(str [,length]) | Determine whether a string ends with a specified string |
The includes method determines whether a specified string is included.
You can also specify the index to start searching in the second argument.
The startsWith method determines whether a string starts with the specified string.
You can also specify the index to start searching in the second argument.
The endsWith method determines whether a string ends with the specified string.
You can also specify the length of the string to search for in the second argument.
let str = "Hello World";
console.log(str.includes("World")); // true
console.log(str.startsWith("Hello")); // true
console.log(str.endsWith("World")); // true
If you not only want to determine whether a substring exists, but also want to know its position, use the indexOf/lastIndexOf methods.
summary
The following is a summary of how to search for a specific string .
- The indexOf method searches from the beginning of the string.
- The lastIndexOf method searches from the end of the string.
The following is a summary of how to determine whether a specific string is included .
- The includes method determines whether the specified string is included.
- The startsWith method determines whether a string starts with the specified string.
- The endsWith method determines whether a string ends with the specified string.
To search for a string, you can use the indexOf and lastIndexOf methods. These methods can be used to easily search for strings, so they can also be used for application development.
It’s easy to use these methods to search and determine whether a string is included!
The indexOf and lastIndexOf methods can be used not only to search strings, but also to search arrays.
Please also try searching for arrays.
Additionally, the includes, startsWith, and endsWith methods can limit the search range by specifying an index in the second argument.
Please use them according to your own program.
Comments