Detailed description of indexOf in javascript, javascriptindexof

Source: Internet
Author: User

Detailed description of indexOf in javascript, javascriptindexof

JavaScript provides several techniques to search for a word, number, or other string of characters in a string. Search may be very convenient, for example, if you want to know which Web browser visitors use to browse your site. Each Web browser identifies its own information in a string, which contains many different statistics. You can add the following JavaScript code in a Web page and preview it in a Web browser to see this string:

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

Navigator is a 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 ). Therefore, if you want to see if the Web browser is IE 7, you can search for "MSIE 7" only in the userAgent string ". One of the methods for searching strings is the indexOf () method. Add a period after the string, then indexOf (), and provide the string you want to search. The basic structure is as follows:
String. indexOf ('string to look ')

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

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

In this example, if indexOf () does not find 'msie' in the userAgent string, it returns-1. Therefore, the conditional test checks whether the result is not equal (! =)-1. When the indexOf () method finds the string to be searched, it returns a number, which is equal to the start position of the string to be searched. The following example makes things clearer:

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

Here, indexOf () searches for the location of 'to be' in the string 'to be, or not To be. A large string starts with 'to be'. Therefore, indexOf () finds 'to be' at the first position '. However, according to programming, the first position is regarded as 0, the second letter (o) is in position 1, and the third letter (in this example, it is a space) is 2.

The indexOf () method starts from the start of a string. You can also use the lastIndexOf () method to start searching at the end of a string. For example, in Shakespeare's famous saying, the word 'be' appears in two locations. Therefore, you can use indexOf () to find the first 'be' and use lastIndexOf () find the last 'be ':

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

In the two examples, if 'be' does not exist in any position in the string, the result will be-1; if there is only one search string instance, indexO f () and lastIndexOf () returns the same value, that is, the position where the search string starts from a large string.

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.