What is the difference between a string intercept slice () substring () substr ()?

Source: Internet
Author: User

Get substring

Slice () substr () substring () does not modify the string itself, they simply return a basic type of string value

var str= ' abcdefghijklmn ';

1. Pass a parameter

If positive: three returns the same result ( from the parameter subscript to the end of the string )

String.slice (3) =string.substr (3) = string.substring (3) =DEFGHIJKLMN

If it is negative:

String.slice ( -3) = String.substr (-3) These two functions convert 3 to 8 (string length plus (-3) =8), which is equivalent to calling slice (8) and substr (8)

SUBSTRING (-3) converts negative numbers to 0 to return string lengths

2. Pass two parameters

SUBSTRING (1,3) from standard 0 to subscript 2 BC

Slice (1,3) from standard 0 to subscript 2 BC

SUBSTR (1,3) starts with subscript 0 to intercept 3 characters (the difference from two) BCD

3. The second parameter is a negative value

SUBSTRING (3,-4) Converts the second argument to 0, and uses it to substring (3,0) Because this method takes the smaller number as the starting position, the larger number as the ending position, and therefore the equivalent of the call to substring (0,3)

SUBSTR (3,-4) also converts the second argument to 0, which means that 0 strings are returned, so it is empty

Slice (3,-4) converts 4 to a value of character length + (-4)

Difference?

1. A positive parameter is the same, from the parameter subscript to the last

2. A negative parameter substring is always converted to 0 slice () substr () to character length + negative value

3. The second negative substring or 0 substr () is also converted to 0, so the output null character

4. Two parameters substr () The second one is the number of characters to intercept

What is the difference between a string intercept slice () substring () substr ()?

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.