|
| JAVASCRIPT substring() METHOD
The substr() method extracts characters from a string between two specified positions and returns the extacted part as new string.
The substring() method takes a required starting position as a number and an optional stop position. If the optional stop position 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.substring(startposition, stopposition[optional]);
Example Without The Optional Stop Position:
<script type="text/javascript">
var text="My name is John Doe.";
document.write(text.substring(3));
</script>
Will output:
Example With The Optional Stop Position:
<script type="text/javascript">
var text="My name is John Doe.";
document.write(text.substring(3, 4));
</script>
Will output:
See Also:
|
|