JavaScript string object entry instance of slice method (for string interception)


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:

parameterinstructions
str_objectString (object) to manipulate
startA 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
endOptional. 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