JavaScript indexof () and LastIndexOf application Method Instance _ basics

Source: Internet
Author: User

IndexOf () method

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

Note: indexOf() methods are sensitive to case sensitivity!

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

Using the previous charat () method

var str = ' www.webclks.com/archives/3309 ';
for (var i=0 i<str.length; i++) {
 if (Str.charat (i) = = '/') {
 alert (i);
 }
}

IndexOf () method

var str = ' www.webclks.com/archives/3309 ';
Alert (Str.indexof ('/')); Check alert from the No. 0 (
str.indexof ('/', 16));//check alert from 16th
(str.indexof (' x '));/no return-1;

IndexOf () Method instance

var str= ' Focus on scripting, sharing script learning materials and learning tips! ';
var s= ' script; The content to query
var i=0//From the start query/
* for
(; Str.indexof (s,i)!=-1;) {
 alert (Str.indexof (s,i));
 I=str.indexof (s,i) +s.length;
}
*/While


(Str.indexof (s,i)!=-1) {
 alert (Str.indexof (s,i));
 I=str.indexof (s,i) +s.length; 
}

LastIndexOf () method

lastIndexOf() method returns the position of the last occurrence of a specified string value, which is searched forward at the specified position in a string.

Note: lastIndexOf() methods are sensitive to case sensitivity!

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

LastIndexOf () Method Instance

var str= ' focuses on Web front-end learning, sharing Web front-end learning materials and learning tips! ';
Alert (Str.indexof (' W ', 0)); Look back at
alert (str.lastindexof (' W ', 24));

If the 2nd value is a negative number, the default is 0来 processing

Summarize

The above is the entire content of this article, I hope that the study of everyone can help, if there are questions can be exchanged message.

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.