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.