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

  • 2020-03-30 04:06:22
  • OfStack

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:

parameter instructions
str_object String (object) to manipulate
start A 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)
length Optional. 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


Related articles: