Jssubstr, substring, and slice instructions Note _ javascript skills

Source: Internet
Author: User
Many articles about the differences between the substr, substring, and slice methods have been found online, and the content is basically the same. Then, I moved the code in one of the articles to a local machine for testing and found that the test results were somewhat different from those in the original article. Many articles about the differences between the substr, substring, and slice methods have been found online, and the content is basically the same. Then, I moved the code in one of the articles to a local machine for testing and found that the test results were somewhat different from those in the original article.

I believe in the code I personally verified, and I will note it down for future reference.

Substr

The Code is as follows:


Document. write ("|" + str. substr (0, 5) + "|" +"
");
Document. write ("|" + str. substr (0) + "|" +"
");
Document. write ("|" + str. substr (5, 1) + "|" +"
");
Document. write ("|" + str. substr (-5, 2) + "|" +"
");
Document. write ("|" + str. substr (-2,-5) + "|" +"
");


Print Effect

| 1, 12345 |
| 1, 123456 |
| 6 |
IE: | 12 | Chrome: | 23 |


Substring

The Code is as follows:


Document. write ("|" + str. substring (0, 5) + "|" +"
");
Document. write ("|" + str. substring (0) + "|" +"
");
Document. write ("|" + str. substring (5, 1) + "|" +"
");
Document. write ("|" + str. substring (-5, 2) + "|" +"
");
Document. write ("|" + str. substring (-2,-5) + "|" +"
");
Document. write ("|" + str. substring (2,-5) + "|" +"
");


Print Effect
| 1, 12345 |
| 1, 123456 |
| 1, 2345 |
| 12 |

| 12 |
Slice

The Code is as follows:


Document. write ("|" + str. slice (0, 5) + "|" +"
");
Document. write ("|" + str. slice (0) + "|" +"
");
Document. write ("|" + str. slice (5, 1) + "|" +"
");
Document. write ("|" + str. slice (-5, 2) + "|" +"
");
Document. write ("|" + str. slice (-2,-5) + "|" +"
");
Document. write ("|" + str. slice (2,-5) + "|" +"
");


Print Effect
| 1, 12345 |
| 1, 123456 |

| 2 |

|
If the expected result is exactly the same as the print result, your basic skills will not be good. If you are somewhat hesitant or the results are unexpected, it will be helpful for me to write more articles.

Summary

Substr

The arg2 of this method and the arg2 of the other two methods have different meanings at all, so they are extracted separately.

When arg1 is <0, different browsers have different results. IE directly changes arg1 to 0, and Chrome subscript reads are changed from the original read from left to right to read from right to left.

Substring and slice

(1) arg2> arg1, that is, when parameter 2 is greater than parameter 1

Substring: automatically reverses the position. The value is greater than that of arg2, and the value is smaller than that of arg1.
Slice: returns an empty string.

(2) processing negative numbers

Substring: Convert the value of a negative number to 0.
Slice: convert a parameter with a negative value to (String Length-parameter value)
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.