Home  >  Article  >  Web Front-end  >  js substring()字符串截取函数_javascript技巧

js substring()字符串截取函数_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:36:251533browse


使用方法:

复制代码 代码如下:

str.substring(start, end)
"String Literal".substring(start, end)


其中“start”是指明要截取字符串的起始位置,该索引从0 开始起算。
“end”是指明要截取字符串的结束位置,该索引从0 开始起算。

JavaScript中substring()函数方法将返回一个包含从start 到最后(不包含end )的子字符串的字符串。

JavaScript中substring()函数方法使用start 和end 两者中的较小值作为子字符串的起始点。例如, strvar.substring(0, 3) 和strvar.substring(3, 0) 将返回相同的子字符串。 如果start 或end为NaN或者负数,那么将其替换为0。子字符串的长度等于start 和end 之差的绝对值。例如,在strvar.substring(0, 3) 和strvar.substring(3, 0) 返回的子字符串的的长度是3。

下面的示例演示了JavaScript中substring函数方法的用法:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
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