Questions about String functions indexof in JavaScript?

Source: Internet
Author: User

<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

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.