Definition and usage:
The substring method is used to extract characters of a string between two specified subscripts.
Syntax:
StringObject. substring (start, end)
Parameter description:
Start is required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.
End is optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringObject. If this parameter is omitted, the returned substring ends at the end of the string.
Return Value:
A new string. The value of this string contains a substring of stringObject. Its content is all characters from start to end-1, and its length is end minus start.
Note:
The substring returned by the substring method includes the start character, but not the end character.
If start and end are equal, this method returns an empty string (a string with a length of 0 ).
If start is greater than end, this method swaps the two parameters before extracting the substring.
If start or end is negative, it is replaced with 0.
Example:
Var str = "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 ));--------""
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.
Note:
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.
Example:
Var str = "0123456789 ";
Alert (str. substr (5); --------------- "56789"
Alert (str. substr (10 ));--------------""
Alert (str. substr (12 ));--------------""
Alert (str. substr (-5); -------------- "0123456789"
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); ------------ "01234"
Alert (str. substr (-1,-5 ));-----------""