JavaScript | String substr()
str.substr() function returns the specified number of characters from the specified index from the given string.
Syntax:
str.substr(start , length)
Arguments:
The first argument to the function start defines the starting index from where the substring is to be extracted from the base string. The second argument to the function length 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 is extracted.
Return value:
This function 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 function are provided below:
Example 1:
var str = 'It is a great day.' print(str.substr(5));
Output:
a great day.
In this example the function 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 function 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 function returns an empty string.
Codes for the above function 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:
- Difference between substr() and substring() in JavaScript
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Difference between String.slice and String.substring
- JavaScript | Insert a string at position X of another string
- How to count string occurrence in string using JavaScript?
- JavaScript | String lastIndexOf()
- JavaScript | string.valueOf()
- JavaScript | string.repeat()
- Reverse a String in JavaScript
- JavaScript | String indexOf()
- How to get nth occurrence of a string in JavaScript ?
- JavaScript | String split()
- JavaScript | String.fromCharCode()
- How to get the last character of a string in JavaScript?
- JavaScript | string.slice()
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.

