English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String substring() 方法

 JavaScript String Object

substring()方法提取字符串的一部分并以新字符串的形式返回,而不修改原始字符串。

开始(start)结束(end)参数指定要提取字符串的一部分。

substring()从start提取字符,但不包括end。尤其是:

  • 如果省略end,则substring()将字符提取到字符串的末尾

  • 如果start等于end,则substring()返回一个空字符串

  • 如果start大于end,则substring()的效果就像两个参数被交换一样

第一个字符的索引为0,第二个字符的索引为1,依此类推。

语法:

string.substring(start, end)
var str1 = 'Air Pollution is the introduction of chemicals into the atmosphere.39;;
var str2 = str1.substring(7);
Test and see‹/›

Browser Compatibility

All browsers fully support the substring() method:

Method
substring()IsIsIsIsIs

Parameter Value

ParameterDescription
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.

Technical Details

Return value:A new string containing the specified part of the given string
JavaScript version:ECMAScript 1

More examples

The following examples use substring() to extract characters from the index4to9(10-1Extract characters from(

var str1 = 'Air Pollution is the introduction of chemicals into the atmosphere.39;;
var str2 = str1.substring(4, 10);
Test and see‹/›

 JavaScript String Object