Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the difference between indexOf and search in JavaScript

黄舟
Release: 2017-12-04 14:17:06
Original
2851 people have browsed it

In our previous article, we introduced to you the detailed explanation of the use of indexof. I believe everyone is familiar with this. Indexof determines whether string It is especially commonly used when including substrings, and is a useful tool for students who are not familiar with regular expressions. Today we will continue to introduce to you the detailed explanation of the difference between indexOf and search in JavaScript!

The IndexOf() method is used to determine whether a string exists in a longer string. Search from the left end to the right end of the long string, and if the substring exists, return its position (i.e. index). If the string being searched for is not found, -1 is returned. Note that the index value should be filled in at the location here. All string indexes start from zero, the first character position is 0, and the end position is the length of the string minus 1.

This method is equivalent to the strstr function in C language and the inStr function in Visual Basic language. This method also has a corresponding function, lastIndexOf(), which searches from the right end of a long string.

Then the problem comes, the search() method also returns the target self-string index value. What is the difference between indexOf() and search()? Why should you use it, and when should you use the search() method?

First of all, it must be clear that the parameters of search() must be regular expressions, while the parameters of indexOf() are just ordinary strings. indexOf() is a lower-level method than search().

If you are just searching for a specific string, then using indexOf() consumes less system resources and is more efficient; if you are searching for a string with certain characteristics (for example, searching for a string that starts with a, followed by is a string of numbers), then indexOf() cannot do anything, and you must use regular expressions and the search() method.

Many times indexOf() is used not because you really want to know the position of the substring, but because you want to know that the substring is not included in the long string. If the returned index value is -1, then it means there is none; if it is not equal to -1, then it means there is.

So generally indexOf is more resource-efficient than search.

Summary:

I believe that through studying this article, you will have a better understanding of the difference between indexOf and search. I hope it will be helpful to your work. Helped!

Related recommendations:

Detailed explanation of the use of indexof in JavaScript


How to use indexOf() and the difference from jQuery.inArray()


##

Detailed explanation of indexOf method in JavaScript array

The above is the detailed content of Detailed explanation of the difference between indexOf and search in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!