
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript String - substr() Method
Description
This method returns the characters in a string beginning at the specified location through the specified number of characters.
Syntax
The syntax to use substr() is as follows −
string.substr(start[, length]);
Argument Details
start − Location at which to start extracting characters (an integer between 0 and one less than the length of the string).
length − The number of characters to extract.
Note − If start is negative, substr uses it as a character index from the end of the string.
Return Value
The substr() method returns the new sub-string based on given parameters.
Example
Try the following example.
<html> <head> <title>JavaScript String substr() Method</title> </head> <body> <script type = "text/javascript"> var str = "Apples are round, and apples are juicy."; document.write("(1,2): " + str.substr(1,2)); document.write("<br />(-2,2): " + str.substr(-2,2)); document.write("<br />(1): " + str.substr(1)); document.write("<br />(-20, 2): " + str.substr(-20,2)); document.write("<br />(20, 2): " + str.substr(20,2)); </script> </body> </html>
Output
(1,2): pp (-2,2): y. (1): pples are round, and apples are juicy. (-20, 2): nd (20, 2): d
javascript_strings_object.htm
Advertisements