English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
substr()The method returns a substring of a specified length starting from a specified position.
The index of the first character is 0, the index of the second character is1and so on.
To extract characters from the end of a string, please use negative index numbers (see the 'More Examples' below).
string.substr(index, length)
var str1 = 'Air Pollution is introduction of chemicals to the atmosphere.'; var str2 = str1.substr(7);Test and See‹/›
All browsers fully support the substr() method:
Method | |||||
substr() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
index | (Required) The index of the first character to include in the returned substring |
length | (Optional) The number of characters to extract. If length is omitted, substr() extracts characters to the end of the string |
Return Value: | A new string containing the specified part of the given string |
---|---|
JavaScript Version: | ECMAScript 1 |
The following example uses substr() from index4Extract characters, length of18:
var str1 = 'Air Pollution is introduction of chemicals to the atmosphere.'; var str2 = str1.substr(4, 18);Test and See‹/›
The following example uses substr() with a negative index:
var str1 = 'www.oldtoolbag.com'; var str2 = str1.substr(-6, 3); // torTest and See‹/›