Home  >  Article  >  Web Front-end  >  举例讲解JavaScript substring()的使用方法_javascript技巧

举例讲解JavaScript substring()的使用方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 15:33:061414browse

定义和用法
substring() 方法用于提取字符串中介于两个指定下标之间的字符。
语法
stringObject.substring(start,stop)

返回值
一个新的字符串,该字符串值包含 stringObject 的一个子字符串,其内容是从 start 处到 stop-1 处的所有字符,其长度为 stop 减 start。
说明
substring() 方法返回的子串包括 start 处的字符,但不包括 stop 处的字符。
如果参数 start 与 stop 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)。如果 start 比 stop 大,那么该方法在提取子串之前会先交换这两个参数。
提示和注释
重要事项:与 slice() 和 substr() 方法不同的是,substring() 不接受负的参数。
实例讲解
例子 1
在本例中,我们将使用 substring() 从字符串中提取一些字符:


输出:lo world!

例子 2
在本例中,我们将使用 substring() 从字符串中提取一些字符:



输出:lo w

以上就是为大家分享的javaScript substring()的用法,希望大家亲自动手试一试,真正掌握javaScript substring()的用法

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