substr関数とsubstring関数は、どちらも特定の「親文字列」から「部分文字列」を抽出するために使用される関数です。ただし、使用法にはいくつかの違いがあります。
長さはオプションです。抽出する「部分文字列」に含まれる文字数を指定します。長さが 0 または負の場合、空の文字列が返されます。このパラメータが指定されていない場合、部分文字列は「親文字列」の末尾まで続きます。
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(10));---------------""
alert(str.substr( 12));--------------""
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: Extract the "substring" from the "start position" to the "end position" from the "mother 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" to the "last character" in the substring.
The substring function returns a substring starting from the "start position" and ending at the "end-1 position" (excluding the characters at the "end position").
The substring function uses the smaller of start and end as the starting point of the substring. For example, stringdata.substring(0,3) and stringdata.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 is equal to the absolute value of the difference between start and end. For example, stringdata.substring(0,3) and stringdata.substring(3,0) return a substring with a length of 3.
The following example demonstrates the use of the substring function.
Example
]
2) If If startIndex and endIndex are equal, an empty string will be returned. If startIndex is larger than endIndex, swap the two parameters before extracting the substring. That is, stringObject.substring(startIndex, endIndex) is equivalent to stringObject.substring(endIndex, startIndex)
The 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
The code is as follows:
Mother string = "Shanghai tap water comes from the sea";
Substring = Mother string.substring(2,4);
//The substring starting from the character "numbered 2" and ending with the character "numbered (4-1)". Return value: "自来"
substr() can be used instead of substring(). From the above code, we can see that stringObject.substr(3,4) is equivalent to stringObject. substring(3,7)