- Defined
The substr () method extracts a specified number of characters from the start subscript in a string.
The substring () method is used to extract the character of a string intermediary between two specified subscripts.
- syntax
substr () stringobject . substr ( start , length )
start required. The starting subscript for the substring to extract. Must be numeric. If it is a negative number, the argument declares the position from the end of the string. 1 refers to the last character in the string, and 2 refers to the second-lowest character.
length optional. The number of characters in the substring. Must be numeric. If this argument is omitted, the string from the beginning of the Stringobject to the end is returned.
substring () stringobject . substring ( Start , stop )
start required. A nonnegative integer that specifies the position of the first character of the substring to be extracted in the stringobject.
stop Optional. A nonnegative integer that is 1 more than the last character of the substring to extract in Stringobject. If this argument is omitted, the returned substring continues until the end of the string.
- Contrast
var a= "abcdefghiklmnopqrstuvwxyz"; var b=a.substr (3,5); var c=a.substring (3,5); The result of the printout is: Defghde Note that the last 5 subscript is not taken to mean that it can only intercept a string of 3, 4 is not intercepted when the subscript is the last [3,5]
JS intercept string substr and substring differences