Comparison of javascript substr and substring usage

Source: Internet
Author: User

Both the substr function and the substring function are used to extract the "substring" from a "parent string. However, there are some differences in usage.

Substr Method

Definition and usage

The substr method returns a substring of the specified length starting from the specified position.

Syntax

StringObject. substr (start [, length])

Parameter description
Start is required. The starting position of the required substring. The index of the first character in the string is 0.
Length is optional. The number of characters to be included in the returned substring.

Description

If start is negative, start = str. length + start.
If the length is 0 or negative, an empty string is returned.
If this parameter is not specified, the substring will continue to the end of the stringObject.

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 sea ";
Substring 1 = substring. substr (2, 4 );
// Extract four substrings of length starting from the character number 2. Return Value: "tap water"
Substring 2 = substring. substr (2 );
// Starts from the character number 2 and ends with the substring of the last character. Return Value: "tap water from sea"


Copy codeThe Code is as follows:
Var stringObject = "hello world! ";
Alert (stringObject. substr (3); // lo world!
Alert (stringObject. substr (3, stringObject. length); // lo world!
Alert (stringObject. substr (3, 4); // lo w

Example:
Copy codeThe Code is as follows:
Var str = "0123456789 ";

Alert (str. substring (0); ------------ "0123456789"
Alert (str. substring (5); ------------ "56789"
Alert (str. substring (10 ));-----------""
Alert (str. substring (12 ));-----------""
Alert (str. substring (-5); ----------- "0123456789"
Alert (str. substring (-10); ---------- "0123456789"
Alert (str. substring (-12); ---------- "0123456789"
Alert (str. substring (0, 5); ---------- "01234"
Alert (str. substring (0, 10); --------- "0123456789"
Alert (str. substring (0, 12); --------- "0123456789"
Alert (str. substring (2, 0); ---------- "01"
Alert (str. substring (2, 2 ));----------""
Alert (str. substring (2, 5); ---------- "234"
Alert (str. substring (2, 12); --------- "23456789"
Alert (str. substring (2,-2); --------- "01"
Alert (str. substring (-01234"
Alert (str. substring (-1,-5 ));--------""

Alert (str. substr (0); --------------- "0123456789"
Alert (str. substr (5); --------------- "56789"
Alert (str. substr (10 ));--------------""
Alert (str. substr (12 ));--------------""
Alert (str. substr (-5); -------------- "56789"
Alert (str. substr (-10); ------------- "0123456789"
Alert (str. substr (-12); ------------- "0123456789"
Alert (str. substr (01234"
Alert (str. substr (0, 10); ------------ "0123456789"
Alert (str. substr (0, 12); ------------ "0123456789"
Alert (str. substr (2, 0 ));-------------""
Alert (str. substr (2, 2); ------------- "23"
Alert (str. substr (2,5); ------------- "23456"
Alert (str. substr (2,12); ------------ "23456789"
Alert (str. substr (2,-2 ));------------""
Alert (str. substr (-1, 5); ------------ "9"
Alert (str. substr (-1,-5 ));-----------""


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.

Example

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

2) If startIndex and endIndex are equal, an empty string is returned. If startIndex is larger than endIndex, replace the two parameters before the substring is extracted. That is, stringObject. substring (startIndex, endIndex) is equivalent to stringObject. substring (endIndex, startIndex)

Copy codeThe Code is as follows:
Var stringObject = "hello world! ";
Alert (stringObject. substring (3, 3); // empty string
Alert (stringObject. substring (3, 7); // lo w
Alert (stringObject. substring (7,3); // lo w


Copy codeThe Code is as follows:
Mother string = "Shanghai tap water comes from 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"


Substr () can be used instead of substring (). The Code above shows that stringObject. substr (3, 4) is equivalent to stringObject. substring (3, 7)

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.