JavaScript str.substr() Function
The str.substr() function returns the specified number of characters from the specified index from the given string.
Syntax:
str.substr(start, len);
Example: This example uses only one parameter for both functions and produces the same output.
Javascript
const str1 = "w3wiki" ; const substrResult = str1.substr(7); const substringResult = str1.substring(7); console.log( "Str.substr(7) =" , substrResult); console.log( "Str.substring(7) =" , substringResult); |
Str.substr(7) = rGeeks Str.substring(7) = rGeeks
Difference between substr() and substring() in JavaScript
In JavaScript Both of the functions are used to get the specified part of the string, But there is a slight difference between them. substr() and substring() are string methods used to extract parts of a string. The main difference is that substr() accepts a length parameter, while substring() accepts start and end indices.