JavaScript indexof syntax and instance usage tutorials

Source: Internet
Author: User

Definitions and usage

The indexof () method returns the location of the first occurrence of a specified string value in a string.

Grammar

Stringobject.indexof (Searchvalue,fromindex)
Parameters Description
Searchvalue Necessary. A string value that requires retrieval.
Fromindex An optional integer parameter. Specify where to start retrieving in the string. Its legal value is 0 to stringobject.length-1. If this argument is omitted, it is retrieved from the first character of the string.

Description

This method retrieves the string stringobject from beginning to end to see if it contains substring searchvalue. The location at which to start retrieving is at the fromindex of the string or at the beginning of the string (no fromindex is specified). If a searchvalue is found, it returns the position of the first occurrence of searchvalue. The character position in the Stringobject is starting at 0.

Tips and comments

Note:the indexof () method is case sensitive!

Note: if the string value to retrieve does not appear, the method returns-1.

The code is as follows Copy Code

var arr = [1,2,3];
Alert (Arr.indexof (1));

Object does not support this property and method. And in Chrome,ff down to run well. So to ask Google great God, found that the original web page effects in the IndexOf method in the js1.6 version only to give the implementation, ie7,8 only to achieve the js1.3 version, Chrome js1.7 version, FF for the js1.8 version. (ie is still half a slow shot). Helpless, had to expand for IE:
Copy code code as follows:

The code is as follows Copy Code
Array.prototype._indexof = function (n) {
if ("indexof" in this) {
return this["IndexOf"] (n);
}
for (Var i=0;i<this.length;i++) {
if (N===this[i]) {
return i;
}
}
return-1;
};

IndexOf Method:

Returns the character position for the first occurrence of a substring within a string object.

Strobj.indexof (substring[, startindex])
Parameters
Strobj
Required option. A string object or text.
Substring
Required option. The substring to find in the string object.
Starindex
Options available. An integer value that indicates the index in which to start the lookup within a string object. If omitted, it is looked up from the beginning of the string.
Description
The IndexOf method returns an integer value that indicates the starting position of the substring of the string object. If no substring is found, returns-1.

If the startindex is a negative number, the startindex is treated as zero. If it is larger than the maximum character position index, it is considered to be the largest possible index.

Performs a lookup from left to right. Otherwise, the method is the same as LastIndexOf.

LastIndexOf Method:

Returns the last occurrence of the substring of a string object.

Strobj.lastindexof (substring[, startindex])
Parameters
Strobj
Required option. A string object or text.
Substring
Required option. The substring to find within the string object.
startindex
Options available. The integer value indicates the starting index position of the lookup within the string object. If omitted, the lookup starts at the end of the string.
Description
The LastIndexOf method returns an integer value that indicates the starting position of the substring of the string object. If no substring is found, returns-1.

If the startindex is a negative number, the startindex is treated as zero. If it is larger than the maximum character position index, it is considered to be the largest possible index.

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.