substr method
Returns a substring of the specified length starting from the specified position.
stringvar.substr(start [, length ])
parameter
stringvar
Required option. A string literal or String object to extract a substring.
start
Required option. 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 continues to the end of stringvar.
Example
The following example demonstrates the usage of the substr method.
function SubstrDemo(){ var s, ss; // Declare variables. var s = "The rain in Spain falls mainly in the plain."; ss = s.substr(12, 5); // Get substring. return(ss); // Return "Spain". }For example: <script type="text/javascript"> var str = "0123456789";// alert(str.substring(0));//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- alert(str.substring(-10));//--------------"0123456789" alert(str.substring(-12));//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- alert(str.substring(2,2));//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- alert(str.substr(5));//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ alert(str.substr(0,10));//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- alert(str.substr(-1,5));//--------------"01234" alert(str.substr(-1,-5));//-------------------"" </script>
substring method
Returns a substring located at the specified position in the String object.
strVariable.substring(start, end)
"String Literal".substring(start, end)
parameter
start
Specifies the starting position of the substring, and the index starts from 0.
end
Specifies the end position of the substring, and the index starts from 0.
illustrate
The substring method returns a string containing the substring from start to the end (not end).
The substring method uses the smaller values of both start and end as the starting point of the substring. For example, strvar.substring(0, 3) and strvar.substring(3, 0) will return the same substring.
If start or end is NaN or a negative number, 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 in strvar.substring(0, 3) and strvar.substring(3, 0) is 3.
Example
The following example demonstrates the usage of the substring method.
function SubstringDemo(){ var ss; // Declare variables. var s = "The rain in Spain falls mainly in the plain.."; ss = s.substring(12, 17); // Take substring. return(ss); // Return the substring "Spain". }The above article in-depth understanding of substr and substring in JS is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.