<script>
var str="abcdefghijklmnopqrstuvwxyz";
document.write("字符串"+str+"中从前到后pq出现的位置是:"+str.indexOf('pq',6)+'<br>');
document.write("字符串"+str+"中从后到前pq出现的位置是:"+str.lastIndexOf('pq',1)+'<br>');
</script>
No matter how the previous code changes (' PQ ', 1) The value of the number, it is unchanged, or 15 and -1!
How is this going to happen?
Question added: document.write ("string" +str+ "in the former to post PQ appears in the position is:" +str.indexof (' PQ ', 6) + ' <br> ');
What is wrong with this sentence! is to find the PQ string starting with the 6th character! Should be able to find Ah! What is 15?
The last answer was wrong here to apologize.
The position of a string in JavaScript starts at 0, and 0 is always the first character on the left.
LastIndexOf is: Starting from the left, the last occurrence of PQ position, when you say the number of changes in the range of less than 15 o'clock, the value is always 1, greater than 15 o'clock, the value is always 15
Instance:
Str.lastindexof (' PQ ', 8) begins with the 8th character I Countdown IHGFEDCBA. Does not appear PQ, return is-1
Str.lastindexof (' PQ ', 25) starting from Z to find PQ from right to left