JavaScript indexOf () method and LastIndexOf () method

Source: Internet
Author: User

One, definition and usage

The IndexOf () method returns the position of the first occurrence of a specified string value in a string.

The LastIndexOf () method returns the location of the last occurrence of a specified string value, which is searched from behind at the specified position in a string.

Grammar
Stringobject.indexof (Searchvalue,fromindex)
Stringobject.lastindexof (Searchvalue,fromindex)

IndexOf () method

Parameters Description
Searchvalue Necessary. Specifies the string value that needs to be retrieved.
Fromindex An optional integer parameter. Specifies 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.

LastIndexOf () method

Parameters Description
Searchvalue Necessary. Specifies the string value that needs to be retrieved.
Fromindex An optional integer parameter. Specifies where to start retrieving in the string. Its legal value is 0 to stringobject. length-1. If this argument is omitted, the search begins at the last character of the string.

return value

LastIndexOf () method if Searchvalueexists before the fromindex position in Stringobject , the last one that appears is returned the location of the Searchvalue.

Description

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

The LastIndexOf () method retrieves the string stringobjectfrom the end of the head to see if it contains a substring searchvalue. The location at which to begin retrieving is at the fromindex of the string or at the end of the string (when no fromindex is specified). If a searchvalueis found, the position of the first character of the Searchvalue in stringobject is returned. The character position in Stringobject is starting at 0.

Second, hints and notes

Note: Both the IndexOf () method and the LastIndexOf () method are case-sensitive!

If the string value that you want to retrieve does not appear, the method returns-1.

Example 1:

The effect is as follows:

Example 2:

The effect is as follows:

(I do not know if the reader will have doubts, why the results are the same?) In this long-winded, because the LastIndexOf () method is a backward search, but the return position is from the beginning of the number and calculation, so the result is as above. )

Download: http://www.cnblogs.com/iceflorence/p/5825286.html

2017-08-26 11:08:00

JavaScript indexOf () method and LastIndexOf () method

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.