The difference between substring (), substr (), slice () in JavaScript

Source: Internet
Author: User

1.stringobject.substring (start,stop) is used to extract the character of a string intermediary between two specified subscripts.

(1) Start required. A nonnegative integer that specifies the position of the first character of the substring to be extracted in the stringobject.

(2) stop is optional. A nonnegative integer that is 1 more than the last character of the substring to extract in Stringobject. If this argument is omitted, the returned substring continues until the end of the string.

(3) Start starts from 0 to stop (does not contain stop) and does not accept negative arguments.

2.stringobject.substr (start,length) to extract a specified number of characters from the start subscript in a string

(1) Start required. The starting subscript for the substring to extract. Must be numeric. If it is a negative number, the argument declares the position from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on.

(2) length is optional. The number of characters in the substring. Must be numeric. If this argument is omitted, then the string from the beginning of the stringobject to the end is returned.

3.stringobject.slice (start,end) extracts a portion of a string and returns the extracted part with a new string

(1) Start required. The starting subscript for the fragment to be extracted. If it is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on.

(2) End optional. The subscript immediately following the end of the fragment to be extracted. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string.

Cases:

(1) var stringvalue = "Hello World";
Alert (Stringvalue.slice (3)); "Lo World"
Alert (stringvalue.substring (3)); "Lo World"
Alert (STRINGVALUE.SUBSTR (3)); "Lo World"
Alert (Stringvalue.slice (3,7)); "Lo W"
Alert (stringvalue.substring (3,7)); "Lo W"
Alert (STRINGVALUE.SUBSTR (3,7)); "Lo worl"

Analysis: The return of the new string includes the string Stringobject all characters starting from start (including start) to end (excluding end); If only one parameter n will return the remaining string from the nth position (starting at 0)
If there are two parameters N,m,slice and substring will return from the nth position to the first m position (excluding the position m), and Substr will return the M characters starting at the nth position.

(2) var stringvalue = "Hello World";

Alert (Stringvalue.slice (-3)); "Rld"

Alert (Stringvalue.substring (-3)); "Hello World"

Alert (STRINGVALUE.SUBSTR (-3)); "Rld"

Alert (Stringvalue.slice (3,-4)); "Lo W"

Alert (stringvalue.substring (3,-4)); "Hel"

Alert (STRINGVALUE.SUBSTR (3,-4)); "" (empty string)

Analysis: When the argument is negative, slice adds the passed-in negative value to the string length (string.length), substr the negative first argument to the string length, and the second to 0,substring converts all negative values to 0.

The JavaScript implementation of IE has a problem with handling negative values to the substr () method, which returns the original string.

The difference between substring (), substr (), slice () in JavaScript

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.