JavaScript slice method
The slice method is used to intercept a part of a string and return that part of the string. The syntax is as follows:
The code copy is as follows:
str_object.replace(start, end)
Parameter description:
| parameter | illustrate |
|---|---|
| str_object | String (object) to operate |
| start | Required. The start position to be intercepted is calculated from 0; if it is a negative number, it starts in reverse from the end of the string. |
| end | Optional. The end position to be intercepted, if omitted, it will end the string; if it is a negative number, it will start to calculate in reverse from the end of the string. |
slice method example
The code copy is as follows:
<script language="JavaScript">
var str = "abcdef";
document.write( str.slice(1) + "<br />" );
document.write( str.slice(1,3) + "<br />" );
// Get the last two characters
document.write( str.slice(-2) + "<br />" );
document.write( str.slice(-4,-2) );
</script>
Run this example and output:
The code copy is as follows:
bcdef
bc
ef
cd