SUBSTR Definitions and usage
The substr () method extracts a specified number of characters from the start subscript in a string.
Grammar
Stringobject.substr (Start,length)
Parameters |
Description |
Start |
Necessary. The starting subscript of the substring to extract. Must be a numeric value. If it is a negative number, the argument declares the position from the tail of the string. In other words,-1 refers to the last character in the string, 2 to the penultimate character, and so on. |
Length |
Optional. The number of characters in the substring. Must be a numeric value. If this argument is omitted, the string from the start position of the stringobject to the end is returned. |
Description
If length is 0 or negative, an empty string is returned. If this argument is not specified, the substring continues to the end of the stringvar.
Example
The following example shows the use of the Substr method.
The first character of the first intercept string.
<script type= "Text/javascript" > var str= "jb51"; Alert (STR.SUBSTR (0,1)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
The result is: j
<script type= "Text/javascript" > Function Substrdemo () {var s, SS;//DECLARE variable. var s = "The rain in Spain falls mainly in the plain."; SS = S.substr (12, 5); Gets the substring. return (SS); Returns "Spain". Alert (Substrdemo ()); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
It means to intercept the string, starting with the 12th character, 5 lengths.
In this case, we'll use substr () to extract some characters from the string:
<script type= "Text/javascript" > var str= "Hello world!" document.write (STR.SUBSTR (3)) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Output:
Lo world!
Example 2
In this case, we'll use substr () to extract some characters from the string:
<script type= "Text/javascript" > var str= "Hello world!" document.write (Str.substr (3,7)) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Output:
Lo Worl