Important: Unlike the slice () and substr () methods, substring () does not accept negative parameters.
The substring (Position1,position2) method is used to extract the character of a string intermediary between two specified subscripts. includes position1 characters, not including Position2 characters (position starting from 0).
Example one:
<script type= "Text/javascript" >var str= "Hello world!" document.write ( str.substring(3)
) </script>
Lo world!
Example two:
<script type= "Text/javascript" >var str= "Hello world!" document.write ( str.substring(3,7)
) </script>
Lo W
Important matters: The methods of the String object Slice (), substring (), and substr () (not recommended) can return the specified part of the string. Slice () is more flexible than substring () because it allows negative numbers to be used as arguments. Slice () differs from substr () because it specifies a substring with a position of two characters, and substr () specifies a substring with a character position and length.
The slice (position1,position2) method extracts a portion of a string and returns the extracted part with a new string. (spaces are also characters) include characters in the position1 position, excluding characters from the position2 position.
Example one:
<script type= "Text/javascript" >var str= "Hello Happy world!" document.write (Str.slice (6)) </script>
Happy world!
Example two:
<script type= "Text/javascript" >var str= "Hello Happy world!" document.write (Str.slice (6,11)) </script>
Happy
Note:the substr () parameter specifies the starting position and length of the substring, so it can be used instead of substring () and slice ().
Important matters: ECMAscript did not standardize the method and therefore objected to its use.
Important: in IE 4, the value of the parameter start is invalid. In this BUG,start Specifies the position of the No. 0 character. In a later release, this bug has been fixed.
The substr (position1,length) method extracts a specified number of characters from a string, starting with the Position1 . length characters that include the position1 position character.
Example one:
<script type= "Text/javascript" >var str= "Hello world!" document.write ( str.substr(3)
) </script>
Lo world!
Example two:
<script type= "Text/javascript" >var str= "Hello world!" document.write ( str.substr(3,7)
) </script>
Lo Worl
JavaScript---function substring (position1,position2), Slice (Position1,position2), substr (position1,length)