Home  >  Article  >  Web Front-end  >  JavaScript字符串对象substr方法入门实例(用于截取字符串)_基础知识

JavaScript字符串对象substr方法入门实例(用于截取字符串)_基础知识

WBOY
WBOYOriginal
2016-05-16 16:33:341157browse

JavaScript substr 方法

substr 方法用于根据开始位置和长度截取字符串并返回截取部分字符串。其语法如下:

复制代码 代码如下:

str_object.substr(start, length)

参数说明:

参数 说明
str_object 要操作的字符串(对象)
start 必需。开始截取的位置,从 0 开始计算;如果为负数则从字符串末尾开始计算(经测试,某些浏览器版本下无效)
length 可选。指要截取的字符串长度,省略则至到字符串结束

提示:substr 方法不符合 ECMAscript 标准,因此不建议使用。

substr 方法实例

复制代码 代码如下:


运行该例子,输出:jb51

复制代码 代码如下:

jb51
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn