JavaScript String substr()
Examples
Extract a substring from text:
let result = text.substr(1, 4);
Start at position 2:
More examples below.
Description
The substr() method extracts a part of a string.
The substr() method begins at a specified position, and returns a specified number of characters.
The substr() method does not change the original string.
To extract characters from the end of the string, use a negative start position.
Warning
The substr() method is removed (deprecated) in the latest JavaScript standard.
Use substring() or slice() instead.
Syntax
Parameters
The start position.
First character is at index 0.
If start is greater than the length, substr() returns "".
If start is negative, substr() counts from the end of the string.
The number of characters to extract.
If omitted, it extracts the rest of the string
Return Value
If length is 0 or negative, an empty string is returned.
More Examples
Only the first:
Only the last:
The last 6:
Browser Support
substr() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |