The substr() method returns a portion of the string, starting at the specified index and extending for a given number of characters afterwards.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
str.substr(start[, length])
Parameters
start- The index of the first character to include in the returned substring.
length- Optional. The number of characters to extract.
Return value
A new string containing the specified part of the given string.
Description
substr() extracts length characters from a str, counting from the start index.
- If
startis a positive number, the index starts counting at the start of the string. Its value is capped atstr.length. - If
startis a negative number, the index starts counting from the end of the string. Its value is capped at-str.length. - Note: In Microsoft JScript, negative values of the
startargument are not considered to refer to the end of the string. - If
lengthis omitted,substr()extracts characters to the end of the string. - If
lengthisundefined,substr()extracts characters to the end of the string. - If
lengthis a negative number, it is treated as0. - For both
startandlength,NaNis treated as0.
Polyfill
Microsoft's JScript does not support negative values for the start index. To use this feature in JScript, you can use the following code:
// only run when the substr() function is broken
if ('ab'.substr(-1) != 'b') {
/**
* Get the substring of a string
* @param {integer} start where to start the substring
* @param {integer} length how many characters to return
* @return {string}
*/
String.prototype.substr = function(substr) {
return function(start, length) {
// call the original method
return substr.call(this,
// did we get a negative start, calculate how much it is from the beginning of the string
// adjust the start parameter for negative value
start < 0 ? this.length + start : start,
length)
}
}(String.prototype.substr);
}
Examples
Using substr()
var aString = 'Mozilla'; console.log(aString.substr(0, 1)); // 'M' console.log(aString.substr(1, 0)); // '' console.log(aString.substr(-1, 1)); // 'a' console.log(aString.substr(1, -1)); // '' console.log(aString.substr(-3)); // 'lla' console.log(aString.substr(1)); // 'ozilla' console.log(aString.substr(-20, 2)); // 'Mo' console.log(aString.substr(20, 2)); // ''
Specifications
| Specification |
|---|
| ECMAScript (ECMA-262) The definition of 'String.prototype.substr' in that specification. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
substr | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support 4 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
Legend
- Full support
- Full support
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.
