javascript indexOf technical details

  • 2020-06-07 04:00:14
  • OfStack

JavaScript provides several techniques to search for a word, number, or other string of characters in a string. Searching can be handy, for example, if you want to know which Web browser your visitors use to browse your site. Each Web browser identifies itself in a string that contains many different statistics. You can see this string by adding the following JavaScript to an Web page and previewing it in Web browser:


 The < script > 
alert ( navigator.userAgent ); 
 The < /script > 

Navigator is an Web browser object, and userAgent is a property of the navigator object. The userAgent attribute contains a long string of information. For example, for Internet Explorer 7 running on Windows XP, the userAgent attribute is: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1). So if you want to see if Web browser is IE 7, you can just search for "MSIE 7" in the userAgent string. The first method to search for strings is the indexOf () method. Add a period after the string, followed by indexOf (), and provide the string you are looking for. The basic structure is as follows:
string.indexOf ('string to look for')

The indexOf () method returns a number: -1 if the search string is not found. So if you want to check Internet Explorer, do this:


var browser=navigator.userAgent ; //this is a string
if ( browser.indexOf ( 'MSIE' )! =-1 ) {
//this is Internet Explorer
}

In this example, if indexOf () does not find 'MSIE' in the userAgent string, it returns -1, so the conditional test checks to see if the result is not equal to (! =) - 1. When the indexOf () method does find the string to search for, it returns a number equal to the starting position of the string to search for. The following example makes things a little clearer 1:


var quote='To be, or not to be.'
var searchPosition=quote.indexOf ( 'To be' ); //returns 0

Here, indexOf () searches for the position 'To be' in the string 'To be, or not to be.' The larger string starts with 'To be', so indexOf () finds 'To be' at the first position. However, programmatically, the first position is considered 0, the second letter (o) is 1, and the third letter (in this case, a space) is 2.

The indexOf () method starts the search from the beginning of the string. You can also use the lastIndexOf () method to search from the end of the string. For example, in Shakespeare's famous diction, the word 'be' appears in two places, so you can use indexOf () to find the first 'be' and use lastIndexOf () to find the last 'be' :


var quote="To be, or not to be."
var firstPosition=quote.indexOf ( 'be' ); //returns 3
var lastPosition=quote.lastIndexOf ( 'be' ); //returns 17

In both cases, if 'be' does not exist anywhere in the string, the result will be -1; If there is only one instance of the search string, indexO f () and lastIndexOf () will return the same value, which is where the search string begins in the larger string.

This is the end of this article, I hope you enjoy it.


Related articles: