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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

 a great day.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

 a gre

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.