Differences between substring and substr in JS

Source: Internet
Author: User

Differences between substring and substr in JS
Substring: This method can have either a parameter or two parameters. (1) One parameter: Example: var str = "Olive"; str. substring (3); Result: "ve" Description: When a substring has only one parameter, the parameter indicates the number of digits of the string to be intercepted and taken directly to the end of the string. (2) Two parameters: var str = "Olive"; 1) Str. substring (3, 4); 2) Str. substring (3, 2); Result: 1) "v" 2) 0 Note: When substring has two parameters, the first parameter indicates the number of the string to be truncated, the second parameter indicates the number of characters intercepted to the string. This is a different part of the character Truncation in C #, which leads to different results. Substr: This method can also have one or two parameters. (1) One parameter: Description: When substr is a parameter, the function is the same as when substring is a parameter. (2) Two parameters: var str = "Olive"; 1) Str. substr (3, 2); 2) Str. substr (3, 4); Result: 1) "ve" 2) "ve" NOTE: When substr has two parameters, the first parameter indicates the number of the string to be truncated, the second parameter indicates the number of characters intercepted. This is the same as the character Truncation in C #. Therefore, in future use, substr is recommended if you want to avoid the interception problem.

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.