1.substring method
Definition and usage
The substring method is used to extract characters in a string between two specified subscripts.
grammar
stringObject.substring(start,stop)
Parameter description
start Required. A non-negative integer that specifies the position of the first character of the substring to be extracted in stringObject.
stop optional. A non-negative integer has one more position in stringObject than the last character of the substring to be extracted. If this parameter is omitted, the returned substring will continue to the end of the string.
Return value
A new string whose value contains a substring of stringObject whose content is all characters from start to stop-1, with a length of stop minus start.
illustrate
The substring method returns a substring including the characters at start, but not the characters at end.
If start and end are equal, then the method returns an empty string (that is, a string of length 0).
If start is larger than end, the method will swap these two parameters before extracting the substring.
If start or end is negative, then it will be replaced with 0.
2.substr method
Definition and usage
The substr method returns a substring of the specified length starting from the specified position.
grammar
stringObject.substr(start [, length ])
Parameter description
start Required. The starting position of the required substring. The index of the first character in the string is 0.
length optional. The number of characters that should be included in the returned substring.
illustrate
If length is 0 or negative, an empty string will be returned.
If this parameter is not specified, the substring will continue to the end of the stringObject.
3. Example
The code copy is as follows:
<script type="text/javascript">
function Demo(){
var str,str;
var s = "Hello Word";
str = s.substring(0, 3); // Take substring.
console.log(str);//====>Hel
str = s.substr(0,3);
console.log(str);//====>Hel
}
</script>