JavaScript string object entry instance of slice method (for string interception)
- 2020-03-30 04:06:28
- OfStack
JavaScript slice method
The slice method is used to intercept a portion of a string and return that portion of the string. The syntax is as follows:
str_object.replace(start, end)
Parameter description:
parameter | instructions |
---|---|
str_object | String (object) to manipulate |
start | A necessity. To intercept the start position from 0 Start counting; If it is a negative number, the calculation begins in reverse at the end of the string |
end | Optional. The end position to intercept, if omitted, to the end of the string; If it is a negative number, the calculation begins in reverse at the end of the string |
Slice method instance
<script language="JavaScript">
var str = "abcdef";
document.write( str.slice(1) + "<br />" );
document.write( str.slice(1,3) + "<br />" );
//Gets the last two characters
document.write( str.slice(-2) + "<br />" );
document.write( str.slice(-4,-2) );
</script>
Run the example and output:
bcdef
bc
ef
cd