IndexOf techniques in JavaScript _javascript skills

Source: Internet
Author: User

JavaScript provides several techniques for searching a string for a word, number, or other string of characters. Search can be handy, for example, if you want to know which Web browser a visitor uses to browse your site. Each Web browser identifies information about itself in a string that contains a number of different statistical data. You can see this string by adding the following JavaScript to a Web page and previewing it in a Web browser:


Navigator is a Web browser object, and UserAgent is a property of the Navigator object. The UserAgent property contains a long string of information, for example, for Internet Explorer 7 running on Windows XP, whose UserAgent property is: mozilla/4.0 (Compatible;msie 7.0;windows NT 5.1). So if you want to see the Web browser as IE 7, you can search only "MSIE 7" in the UserAgent string. One way to search for a string is the IndexOf () method. Add a period after the string, then indexof (), and provide the string you want to find. The basic structure looks like this:
String.IndexOf (' String to look for ')

The IndexOf () method returns a number: If the search string is not found, the method returns-1. So, if you want to check Internet Explorer, you can 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 will see if the result is not equal to (!). =)-1. When the indexof () method does find the string to search for, it returns a number that equals the starting position of the string to find. The following example makes things clearer:

var quote= ' to be, ' or ' does not '
. var searchposition=quote.indexof (' to be ');//returns 0

Here, indexOf () searches for the position of ' to be ' in the string ' to be, or '. The larger string starts with ' to be ', so indexOf () finds the ' to IS ' in the first position. However, programmatically, the first position is considered to be 0, the second letter (O) at position 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 start the search from the end of the string. For example, in Shakespeare's famous sayings, 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 being," or not to being. "
var firstposition=quote.indexof (' being ');//returns 3
var lastposition=quote.lastindexof (' be ');//returns 17

In these two examples, if ' be ' does not exist anywhere in the string, the result will be-1; if there is only one instance of a search string, Indexo f () and LastIndexOf () will return the same value, where the search string begins in a larger string.

The above is the entire contents of this article, I hope you can enjoy.

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.