The usage and difference of SUBSTRING,SUBSTR and splice in JS

Source: Internet
Author: User

I. Definition and usage

1.substring

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

The substring () method returns a substring that includes the character at the beginning , but does not include the character at the end .

syntax: string. substring (from, to)
Parameters Description
From Necessary. A nonnegative integer that specifies the position of the first character of the substring to extract in the string Object. (negative parameters are converted directly to 0)
To Optional. A non-negative integer that is 1 more than the last character of the substring to extract in the position of the string Object.
If this argument is omitted, the returned substring continues until the end of the string.

Attention:

1.strvar.substring (from[, to]) will return a string containing a substring from the From and to (without to).
The 2.substring method uses the smaller values from both from and to as the starting point for the substring. For example, strvar.substring (0, 3) and strvar.substring (3, 0) will return the same substring.
3. If from or to is NaN or negative, replace it with 0.
4. The length of the substring equals the absolute value of the difference between the From and to. For example, the length of the substring returned in strvar.substring (0, 3) and strvar.substring (3, 0) is 3.

2.splice

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

Use the start (contains) and end (not included) parameters to specify the part of the string extraction.

The first character position in the string is 0, the second character position is 1, and so on.

Tip: 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. = a positive index that adds this negative number to the length of the string.

syntax: string. Slice ( start, end)parameter Values
Parameters Description
Start Have to. The starting subscript for the fragment to be extracted. The first character position is 0
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 (= positive index that adds the negative number to the length of the string).
return value
type Description
String The extracted string

3.substr

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

tip: the substr () parameter specifies the starting position and length of the substring, so it can be used instead of substring () and slice ().
In IE 4, the value of the parameter start is invalid. In this BUG, start specifies the position of the No. 0 character. In a later release, this bug has been fixed.
ECMAscript did not standardize the method and therefore objected to its use.

Note: the substr () method does not change the source string.

Syntax:string. substr (start,length)

parameter Values
Parameters Description
Start Necessary. 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 (= The result of adding this negative argument to the length of the string as the first argument ).
Length Optional. The number of characters in the substring. Must be numeric. If length is 0 or negative, an empty string is returned, and if the argument is omitted, the string from the beginning of the stringobject to the end is returned.
return value

type Description
String A new string containing the extracted part of the text

Attention:

Stringvar.substr (start [, length])

Two, different points

1. The second parameter is different

2. When the received index parameter is negative:

Slice will add the length of its string to the corresponding negative number, and the result will be the parameter;

SUBSTR is simply the first parameter that adds the first parameter to the length of the string;

The substring simply converts the negative parameters directly to 0.

Three, the same point

If you just write a parameter, both of the two functions are the same: the string fragment after the current subscript is truncated until the end of the string.

The usage and difference of SUBSTRING,SUBSTR and splice in JS

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.