JavaScript substr and SUBSTRING usage compare _javascript techniques

Source: Internet
Author: User

Both the SUBSTR function and the SUBSTRING function are functions used to extract a "substring" from a "parent string". But the usage is somewhat different, the following is introduced separately

substr Method

Definitions and usage

The Substr method returns a substring of the specified length starting at the specified position.

Grammar

Stringobject.substr (start [, length])

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

Description

If start is a negative number, then Start=str.length+start.
If length is 0 or negative, an empty string is returned.
If this argument is not specified, the substring continues to the end of the stringobject.

Function: Extracts the substring of the specified length from the specified location in the parent string.

How to use:

Copy Code code as follows:
String data. substr (start [, length])

Start is a required option. Indicates the starting position of the substring to extract. The index of the first character in the string is 0.
Length is optional. Indicates the number of characters that should be included in the substring to extract. If length is 0 or negative, an empty string is returned. If this argument is not specified, the substring continues to the end of the "parent string."
The following example shows the use of the SUBSTR function.
Copy Code code as follows:

The parent string = "Shanghai tap water comes from the sea";
Substring 1 = female string. substr (2,4);
Extracts 4-length substrings starting with a character numbered 2. Return value: "Tap water"
Substring 2 = female string. substr (2);
Starts with a character numbered 2 to a substring of the last character. Return value: "Tap water from the sea"


Copy Code code 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 Code code as follows:

var str = "0123456789";

Alert (str.substring (0));------------"0123456789"
Alert (str.substring (5));------------"56789"
alert (str.substring);-----------""
alert (str.substring);-----------""
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 ( -1,5));---------"01234"
Alert (str.substring ( -1,-5));--------""

Alert (STR.SUBSTR (0));---------------"0123456789"
Alert (STR.SUBSTR (5));---------------"56789"
alert (STR.SUBSTR);--------------""
alert (STR.SUBSTR);--------------""
Alert (STR.SUBSTR ( -5));--------------"56789"
Alert (STR.SUBSTR ( -10));-------------"0123456789"
Alert (STR.SUBSTR ( -12));-------------"0123456789"
Alert (STR.SUBSTR (0,5));-------------"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: Extracts the substring from start position to end position from the "parent string".

How to use: String data. substring (start,end)

The start argument indicates the position of "first character" in the substring.
The end parameter indicates the position of the "one character after the last character" in a substring.
The SUBSTRING function returns a substring of a character starting at the start position to the end of the "end-1 position" that does not contain the ending position.
The SUBSTRING function uses the smaller values in both start and end as the starting point for substrings. For example, string data. substring (0,3) and string data. Substring (3,0) will return the same substring.

If start or end is NaN or negative, replace it with 0.

The length of the substring equals the absolute value of the difference between start and end. For example, string data. substring (0,3) and string data. Substring (3,0) The length of the substring returned is 3.


The following example shows the use of the SUBSTRING function.

Cases
<script type= "Text/javascript" > var stringobject = "Hello world!"; Alert (stringobject.substring (3)); Lo world! Alert (stringobject.substring (3,stringobject.length)); Lo world! Alert (stringobject.substring (3,7)); Lo W, Space also counted [l][o][][w] </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


2 if the startindex and endindex are equal, the empty string is returned. If the startindex is larger than the Endindex, swap two parameters before extracting the substring. i.e. stringobject.substring (Startindex,endindex) is equivalent to stringobject.substring (Endindex,startindex)

Copy Code code 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 Code code as follows:

The parent string = "Shanghai tap water comes from the sea";
substring = female string. substring (2,4);
A substring that begins with the character "number 2" to the end of the character numbered (4-1). Return value: "Unsolicited"


substr () can be used instead of substring (), which shows that stringobject.substr (3,4) is equivalent to Stringobject.substring (3,7) in the code above

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.