indexOf() method
indexOf() method returns the first occurrence of a specified string value in the string.
Note: indexOf() method is case sensitive!
Note: If the string value to be retrieved does not appear, the method returns -1.
Use 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 0th (str.indexOf('/',16)); // Check alert from the 16th (str.indexOf('x')); // Can't find return -1;indexOf() method example
var str='Concentrate on script learning, share script learning materials and learning experiences and skills! ';var s='Script; // The content to be queryed var i=0; // Start from the number of times /*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 can return the last location of the specified string value, searching from back to front at the specified location in a string.
Note: lastIndexOf() method is case sensitive!
Note: If the string value to be retrieved does not appear, the method returns -1.
LastIndexOf() method example
var str='Concentrate on the web front-end learning and share the web front-end learning materials and learning experiences! ';alert(str.indexOf('W',0)); // Check alert(str.lastIndexOf('W',24)); // Check from behindIf the second value is negative, it is processed as 0 by default
Summarize
The above is all the content of this article. I hope it will be helpful to everyone's study and work. If you have any questions, you can leave a message to communicate.