Analysis of substring (), slice (), and substr () Methods

Source: Internet
Author: User

I have read the previous documents these days. When I see substring (), slice (), and substr (), I am not clear about the differences between them. After reading the materials and verifying them in person, I will make the following summary:

First, define a variable to facilitate the following test: var STR = 'q1207526854 'Str. substring (form, to): truncates a string with the subscript form to the subscript to (excluding the characters corresponding to) Alert (Str. substring (2075) // when form> to, substring takes a smaller parameter as the first parameter alert (Str. substring (6, 2) // 2075 and when one of the parameter values is negative, substring directly converts the negative number to 0. Alert (Str. substring (6,-2) // q12075 reverse Str. slice (Star, end): extracts from a string whose subscript is star to whose subscript is end (excluding the character corresponding to end) Alert (Str. slice (2075) // when star> end, slice returns an empty string alert (Str. slice (6, 2) // null when one of the parameter values is negative, slice will add the result of this parameter and the string length as the parameter alert (Str. slice (6,-2) // 268 and slice can operate on arrays. ---------------------------------------------------- Str. substr (Star, length): starting from a string whose subscript is star, truncates a substring of length, alert (Str. substr (207526) // when star is a negative number, substr adds the result of this parameter to the string length as the parameter alert (Str. substr (-6, 2) // 75 returns an empty string when the end value is negative. Alert (Str. substr (6,-2) // null

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.