|
| JAVASCRIPT substr() METHOD
The substr() method extracts characters from a string where the extraction starts from a specified starting position and returns the extacted part as new string.
The substr() method takes a required starting position as a number and an optional desired string length of the extracted part. If the desired string length is not specified, then the extracted string will be from starting position to end of main string.
Remember: the first position of a string is 0 and not 1.
The general format is:
string.substr(startingposition, length[optional]);
Example Without The Optional Length:
<script type="text/javascript">
var text="My name is John Doe.";
document.write(text.substr(3));
</script>
Will output:
Example With The Optional Length:
<script type="text/javascript">
var text="My name is John Doe.";
document.write(text.substr(3, 4));
</script>
Will output:
See Also:
|
|