Differences between substring (), substr (), and slice () in javascript, substringsubstr

Source: Internet
Author: User

Differences between substring (), substr (), and slice () in javascript, substringsubstr

StringObject. substring (start, stop)It is used to extract characters that are intermediary between two specified subscripts.

Start is required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.

Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringObject. If this parameter is omitted, the returned substring ends at the end of the string.

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

StringObject. substr (start, length)You can extract a specified number of characters starting with the start subscript from a string.

Start is required. The starting subscript of the substring to be extracted. It must be a numerical value. If it is a negative number, this parameter declares the position starting from the end of the string. That is to say,-1 refers to the last character in the string,-2 refers to the second to last character, and so on.

Length is optional. The number of characters in the substring. It must be a numerical value. If this parameter is omitted, the string from the start position of the stringObject to the end is returned.

StringObject. slice (start, end)Extract A part of the string and return the extracted part with the new string.

The starting subscript of the part to be extracted by start. If it is a negative number, this parameter specifies the position starting from the end of the string. That is to say,-1 refers to the last character of the string,-2 refers to the second to last character, and so on.

The subscript next to the end of the clip 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 this parameter is a negative number, it specifies the position starting from the end of the string.

Returns a new string that contains all the characters starting from start to end (excluding end) of the stringObject string.

string.slice()string.substring()string.substr() 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”

If the three have only one parameter n, the remaining string is returned from the nth position (from 0)

If there are two parameters: n, m, slice, and substring, the string from the nth position is returned to the nth position (excluding the nth position, substr returns the m characters starting from the nth position.
-------------------------------
 

String. slice () string. substring () string. substr () 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); // "El" alert (stringValue. substr (3,-4); // "(empty string)

When the parameter is a negative value, slice will pass in a negative value and a string length (string. length), substr will add the string length to the negative first parameter, the second is converted to 0, and the substring will convert all negative values to 0.

The JavaScript Implementation of IE has a problem when processing the negative value passed to the substr () method. It returns the original string.

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.