Comparison of javascript substr and substring usage

Source: Internet
Author: User

Substr Function
Function: extract the "substring" of "specified length" from "specified position" of "Parent string ".
Usage: Copy codeThe Code is as follows: String data. substr (start [, length])

Start is required. Specifies the starting position of the "substring" to be extracted. The index of the first character in the string is 0.
Length is optional. Specifies the number of characters to be included in the "substring" to be extracted. If the length is 0 or negative, an empty string is returned. If this parameter is not specified, the sub-string continues until the end of the "parent string.
The following example demonstrates the substr function usage.Copy codeThe Code is as follows: Mother string = "Shanghai tap water comes from the sea ";
Substring 1 = substring. substr (2, 4 );
// Extract four substrings of length starting from the character number 2. Returned value: "Tap Water" substring 2 = parent string. substr (2 );
// Starts from the character number 2 and ends with the substring of the last character. Return Value: "tap water from sea"

Substring Function
Function: extract the "substring" from the "starting position" to the "Ending position" from the "parent string ".
Usage: String data. substring (start, end)
The start parameter specifies the position of the "first character" in the substring.
The end parameter specifies the position of the "next character" of the "last character" in the substring.
The substring function returns a substring from the "start position" to the "end-1" end (excluding the "end position" characters.
The substring function uses a small value in start and end as the starting point of the substring. For example, string data. substring () and string data. substring () will return the same substring.
If start or end is NaN or negative, replace it with 0.
The length of the substring is equal to the absolute value of the difference between start and end. For example, the length of the substring returned by string data. substring () and string data. substring () is 3.
The following example demonstrates the substring function usage.Copy codeThe Code is as follows: Mother string = "Shanghai tap water comes from the sea ";
Substring = parent string. substring (2, 4 );
// The substring starting from the character "2" and ending with the character "4-1. Returned value: "self-supplied"

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.