JavaScript substring method
The substring method is used to intercept a string by specifying a start and end position and to return an intercept part of the string. The syntax is as follows:
Copy Code code as follows:
Str_object.substring (start, end)
| Parameters |
Description |
| Str_object |
The string to manipulate (object) |
| Start |
Necessary. Start intercept position, nonnegative integer |
| End |
Optional. The position of the end of the string interception, a nonnegative integer, or, if omitted, until the end of the string |
Tip: If the parameter start is equal to end, then the method returns an empty string. If start is larger than end, the method exchanges the two parameters before intercepting the string.
Substring Method Instance
Copy Code code as follows:
<script language= "JavaScript" >
var str = "ABCdef";
document.write (str.substring (1, 3) + "<br/>");
document.write (Str.substring (2, 2) + "<br/>");
document.write (Str.substring (3, 1));
</script>
Run this example, output:
Copy Code code as follows:
The difference between substring and slice and substr
The 1.substring cannot accept negative parameters, and the slice method can
2.substring The second parameter is the position at which the end of the interception is specified, and the second parameter of the Substr method is the length of the specified string interception