Differences between substr, substring, and slice. splice in javascript

Source: Internet
Author: User

The substr () method extracts a specified number of characters starting with the start subscript from a string.

StringObject. substr (start, length); start is required. length is optional.

Start is the subscript of the starting position of the truncation. It starts from 0 and must be a number. It can be a negative number,-1 is the first to last,-2 is the second to last, and so on.

Length is the length of the character to be truncated. It must be a number. If not specified, it is truncated from the start position to the end of the string.

Substr specifies the start subscript and truncation length of the string, so it can be used instead of substring and slice.

Important: ECMAscript does not standardize this method, so it is opposed to using it.

The substring () method is used to extract characters of a string between two specified subscripts.

StringObject. substring (start, end); start is required. end is optional.

Start is the subscript of the starting position of the truncation. It must be a non-negative number starting from 0. (w3c says it must be a non-negative integer. It is acceptable if it is not an integer .)

The end must be a number. If it is not specified, it is truncated from the start position to the end of the string.

Note: The characters intercepted by substring do not include the characters at the end. Therefore, the truncation length is end-start.

If start = end, an empty string with a length of 0 is returned.

Important: substring does not receive negative parameters. slice and substr can.

The slice () method can extract a part of a string and return the extracted part with a new string.

StringObject. slice (start, end); start is required. end is optional.

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.

The splice () method is used to insert, delete, or replace elements in an array.

ArrayObject. splice (index, howmany, element1,..., elementX) index, howmany required, other optional.

Index specifies where to add/delete elements. This parameter is the subscript of the array element that begins to insert or delete. It must be a number.

Howmany specifies how many elements should be deleted. It must be a number, but it can be "0 ". If this parameter is not specified, all elements starting from index to the end of the original array are deleted.

Element1 specifies the new elements to be added to the array. Insert from the subscript indicated by index.

Elementx can add several elements to the array.

Note: The splice () method and slice () method have different functions. The splice () method directly modifies the array.

All tips: in some cases, negative parameters are not recognized. Therefore, do not use negative numbers as parameters. This prevents browser incompatibility and program errors.

This is a description in the JavaScript authoritative guide. E-text like me can barely understand it. It is not translated, but explained as follows.

String. Substring (From,To)

Arguments

From

A nonnegative integer that specifies the positionStringOf the first character of the desired substring.

Specify the start position of the string to be obtained, that is, the index (non-negative integer)

To

A nonnegative optional integer that is one greater than the position of the last character of the desired substring. If this argument is omitted, the returned substring runs to the end of the string.

Specifies the end position of the string to be obtained, excluding the characters at this position (non-negative integer, optional, if not specified, returns from the specified start position to the end position of the original string)

String. Slice (Start,End)

Arguments

Start

The string index where the slice is to begin. if negative, this argument specifies a position measured from the end of the string. that is,-1 indicates the last character,-2 indicates the second from last character, and so on.

Specify the start position of the string, that is, the index.

End

The string index immediately after the end of the slice. If not specified, the slice should des all characters fromStartTo the end of the string. If this argument is negative, it specifies a position measured from the end of the string.

Specifies the end position of the string to be obtained, excluding the characters at this position (optional, return from the specified start position to the end position of the original string if not specified)

String. Substr (Start,Length)

Arguments

Start

The start position of the substring. if this argument is negative, it specifies a position measured from the end of the string:-1 specifies the last character,-2 specifies the second-to-last character, and so on.

Specify the start position of the string, that is, the index.

Length

The number of characters in the substring. If this argument is omitted, the returned substring contains des all characters from the starting position to the end of the string.

Specify the length of the string to be obtained. (optional. If no value is specified, the return value ranges from the specified start position to the end position of the original string)

PS: all three methods return a part of the new string of the original string. All the 2nd parameters are optional and all parameters can be negative integers.

String. Substring (From,To)
String. Slice (Start,End)

The two methods are similar. Both specify the start and end positions to return a new string. The return results are the same when the parameters are both positive integers. When the parameters are negative integers,String. Substring (From,To) Treat all negative integers as 0, whileString. Slice (Start,End) Adds a negative integer to the length of the string.

String. Substr (Start,Length)

This method only specifies the length of the new string on the second parameter.String. Slice (Start,EndAdd the negative integer to the length of the original string.
Example

Copy codeThe Code is as follows: var s = "abcdefg ";

S. substring (1, 4) // Returns "bcd"
S. slice (1, 4) // Returns "bcd"
S. substr (1, 4) // Returns "bcde"

S. substring (2,-3) // The Returns "AB" is actually a smaller parameter of s. substring ().
S. slice (2,-3) // Returns "cd" is actually s. slice (2, 4)
S. substr (2,-3) // Returns "cdef" is actually s. slice (2, 4)

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.