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"