JavaScript string object entry instance of the substr method (for intercepting strings)


JavaScript substr method

The substr method is used to intercept a string based on its starting position and length and to return a partially truncated string. The syntax is as follows:

str_object.substr(start, length)

Parameter description:

parameterinstructions
str_objectString (object) to manipulate
startA necessity. Start intercepting the position from 0 Start counting; If it is negative, it starts at the end of the string (which is not valid in some browser versions)
lengthOptional. Indicates the length of the string to be truncated, and omits to the end of the string

Tip: the substr method does not conform to the ECMAscript standard, so it is not recommended.

Instance of the substr method

<script language="JavaScript">
var str = "www.jb51.net";
document.write( str.substr(4,5) );
</script>

Run the example, output: jb51

jb51