Description of Substr,substring,slice.splice in JavaScript-basic knowledge

Source: Internet
Author: User

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

Stringobject.substr (start,length); start must, length optional.

Start is the subscript for the starting position of the intercept and must be a number starting from 0. Can be negative,-1 is the penultimate character,-2 is the penultimate character, and so on.

Length is the size of the character to intercept, and must be a number. If not specified, it is intercepted at the start position to the end of the string.

SUBSTR Specifies the start subscript and intercept length of the string, so it can be used in place of substring and slice.

Important:ECMAscript does not standardize the method and is therefore opposed to its use.

The substring () method extracts characters from a string mediation between two specified subscripts.

Stringobject.substring (start,end); start must, end optional.

Start is the subscript for the starting position of the Intercept, starting from 0, must be a non-negative number. (The consortium says it has to be a nonnegative integer, and it's not an integer.)

End must be a number. If not specified, it is truncated at the start position to the ending of the string.

Note: The characters intercepted by substring do not include the characters at the end. So the length of the interception is End-start.

Start=end, returns an empty string with a length of 0.

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

The slice () method extracts a part of the string and returns the extracted part with a new string.

Stringobject.slice (start,end); start must, end optional.

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.

The splice () method inserts, deletes, or replaces the elements of an array.

Arrayobject.splice (index,howmany,element1,....., elementx) Index,howmany must be, other optional.

Index stipulates where to add/remove elements from. This parameter is the subscript for the array element that starts inserting and/or deleting, and must be a number.

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

Element1 the new element to be added to the array. Start the insertion at the lower mark that is indicated by index.

Elementx can add several elements to an array.

Note: Notice that the splice () method is different from the slice () method, and the splice () method is modified directly to the array.

All tips: In some cases, negative numbers are not recognized. So try not to use negative numbers as arguments. Lest the browser is incompatible, resulting in errors in the program.


This is the instructions on the JavaScript authoritative guide, like my e-text very bad can barely understand, and not to the translation, just according to understand the next.

string. substring (from, to)

Arguments

From

A nonnegative Integer that specifies the position within string of the ' the ' of the character of the desired .

Specifies that you want to get the starting position of the string, that is, the index (nonnegative integer)

To

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

Specifies that you want to get the end position of the string, excluding the character at that position (nonnegative integer, optional, not specified, returning from the specified start to the end 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 to the end of the string. That's,-1 indicates the last character,-2 indicates the second from last character, and.

Specifies where you want the string to begin, that is, the index.

End

The string index immediately after the end of the slice. If not specified, the "slice includes all characters" from " start to" end of the string. If This argument is negative, it specifies a position measured from the end of the string.

Specifies that you want to get the end position of the string, excluding the character at that position (optionally, without specifying, return from the specified start to the end of the original string)

string. substr (start, length)

Arguments

Start

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

Specifies where you want the string to begin, that is, the index.

Length

The number of characters in the substring. If This argument was omitted, the returned substring includes all characters from the starting position to the ' end of the ' s Tring.

Specifies the length of the string you want to get, (optionally, if not specified, return from the specified start position to the end of the original string)



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

string. substring (from, to)
string. Slice (start, end)

The two methods are similar to specifying that the start and end positions return the new string, which returns the result when the argument is a positive integer, and when the argument is a negative integer,string.substring The negative integer is treated as 0, and string. Slice (start, end ) treats the negative integer with the length of the string.

string. substr (start, length)

This method only specifies the length of the new string on the second argument, and the negative integer plus the length of the original string for negative positive and string. Slice (start, end) processing.
Example

Copy Code code 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)//Returns "AB" is actually s.substring (0,2) The smaller parameters will be in the front
S.slice (2,-3)//Returns "CD" is actually S.slice (2,4)
S.SUBSTR (2,-3)//Returns "Cdef" is actually s.slice (2,4)

Related Article

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.