The difference _javascript skill of substring (), substr (), slice () in JavaScript

Source: Internet
Author: User
Tags numeric value

stringobject.substring (start,stop) is used to extract characters from a string mediation between two specified subscripts.

Start Required. A non-negative integer that stipulates the position of the first character of the substring to be extracted in the stringobject.

Stop is optional. A non-negative integer that is 1 more than the last character of the substring to extract in the Stringobject. If this argument is omitted, the returned substring continues to the end of the string.

Start starts at 0 to stop (without stop) and does not accept negative arguments.

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

Start Required. The starting subscript of the substring to extract. Must be a numeric value. If it is a negative number, the argument declares the position from the tail of the string. In other words,-1 refers to the last character in the string, 2 to the penultimate character, and so on.

Length is optional. The number of characters in the substring. Must be a numeric value. If this argument is omitted, the string from the start position of the stringobject to the end is returned.

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

The starting subscript of the fragment to be extracted by start. If it is a negative number, the parameter sets the position from the tail of the string. In other words,-1 refers to the last character of the string,-2 to the penultimate character, and so on.

End is immediately followed by the bottom of the fragment to be extracted. If this parameter is not specified, the substring to be fetched includes the string at the end of the original string. If the argument is a negative number, it sets the position from the tail of the string.

Returns a new string that includes all characters of the string stringobject from start (including start) to end ending (excluding ends)

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"

Three if only one parameter n will return the remaining string starting from nth position (position calculated from 0)

If there are two parameters N,m,slice and substring will return to the first m position (excluding the M position) from the nth position, SUBSTR will return the M character starting at 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));    "Hel"
alert (Stringvalue.substr (3,-4));       "" (empty string)

When the argument is negative, slice adds the incoming 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.

IE's JavaScript implementation is problematic when handling negative values to the substr () method, which 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.