English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
substring()method extracts a part of the string and returns it as a new string without modifying the original string.
instart (start)andend (end)parameters specify the part of the string to be extracted.
substring() fromstartextracts characters, but does not includeend. Especially:
If omittedendthen substring() extracts the characters to the end of the string
Ifstartequal toendthen substring() returns an empty string
Ifstartgreater thanendthen the effect of substring() is as if the two parameters have been swapped
The index of the first character is 0, the index of the second character is1and so on.
string.substring(start, end)
var str1 = 'Air Pollution is the introduction of chemicals to the atmosphere.39;; var str2 = str1.substring(7);Test and see‹/›
All browsers fully support the substring() method:
Method | |||||
substring() | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
start | (Required) The index of the first character to include in the returned substring |
end | (Optional) The index of the first character to exclude from the returned substring. If omittedendIf so, substring() extracts 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 substring() to extract characters from the index4to9(10-1Extract characters from:
var str1 = 'Air Pollution is the introduction of chemicals to the atmosphere.39;; var str2 = str1.substring(4, 10);Test and see‹/›