Below is the example of the String substr() Method.
- Example:
<script>functionfunc() {varstr ='Geeks for Geeks';varsub_str = str.substr(6);document.write(sub_str);}func();</script>chevron_rightfilter_none - Output:
for Geeks
str.substr() method returns the specified number of characters from the specified index from the given string.
Syntax:
str.substr(start , length)
-
Perameters:
- start: It defines the starting index from where the substring is to be extracted from the base string.
- length: It defines the number of characters to be extracted starting from the start in the given string. If the second argument to the function is undefined then all the characters from the start till the end of the length are extracted.
Return value:
This method returns a string that is the part of the given string. If the length is 0 or negative value then it returns an empty string.
Examples for the above method are provided below:
Example 1:
var str = 'It is a great day.' print(str.substr(5));
Output:
a great day.
In this example the method substr() creates a substring starting from index 5 till the end of the string.
Example 2:
var str = 'It is a great day.' print(str.substr(5,6));
Output:
a gre
In this example the method substr() extracts the substring starting at index 5 and length of string is 6.
Example 3:
var str = 'It is a great day.' print(str.substr(5,-7));
Output:
In this example since the length of the string to be extracted is negative therefore the method returns an empty string.
Codes for the above method are provided below:
Program 1:
<script> // JavaScript to illustrate substr() function function func() { // Original string var str = 'It is a great day.'; var sub_str = str.substr(5); document.write(sub_str); } func(); </script> |
Output:
a great day.
Program 2:
<script> // JavaScript to illustrate substr() function function func() { // Original string var str = 'It is a great day.'; var sub_str = str.substr(5,6); document.write(sub_str); } func(); </script> |
Output:
a gre
Program 3:
<script> // JavaScript to illustrate substr() function function func() { // Original string var str = 'It is a great day.'; var sub_str = str.substr(5,-7); document.write(sub_str); } func(); </script> |
Output:
Recommended Posts:
- TypeScript | String substr() Method
- Difference between substr() and substring() in JavaScript
- PHP | substr() function
- Node.js | substr() function
- How to count string occurrence in string using JavaScript?
- JavaScript | Difference between String.slice and String.substring
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Insert a string at position X of another string
- JavaScript String startsWith() Method
- JavaScript String toUpperCase() Method
- JavaScript String toLowerCase() Method
- JavaScript String split() Method
- JavaScript String indexOf() Method
- JavaScript String endsWith() Method
- JavaScript String concat() Method
- JavaScript String.fromCharCode() Method
- JavaScript String trim() Method
- JavaScript String.prototype.charCodeAt() Method
- JavaScript String includes() Method
- JavaScript string.toString() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


