在JavaScript String对象中,slice、substring两个函数提供了字符串的截取功能;substr函数提供了字符串的抽取功能。顾名思义,前二个函数在于截取字符串、后一个函数在于抽取字符串。 二.参数解析对于大部分程序设计语言来说,截取字符串一般会提供至少一个起始字符索引位置和一个结束字符索引位置。计算机的索引一般都是以0开始的(数组的索引、集合的遍历等等);抽取字符串更容易理解一些,通用的理解就是你给我一个起始位置,以及要抽取字符的个数,我就能给你抽取指定长度的字符串了。 三.函数对比
四.示例演示
1 var str = "abc lmx xyz";
2
3 console.log("slice(2):", str.slice(2));
4 console.log("slice(2,6):", str.slice(2, 6));
5 console.log("slice(-2):", str.slice(-2));
6 console.log("slice(-6,-2):", str.slice(-6, -2));
7 //打印:slice(2): c lmx xyz
8 //打印:slice(2,6): c lm
9 //打印:slice(-2): yz
10 //打印:slice(-6,-2): mx x
11
12 console.log("substring(2):", str.substring(2));
13 console.log("substring(2,6):", str.substring(2, 6));
14 //打印:substring(2): c lmx xyz
15 //打印:substring(2,6): c lm
16
17 console.log("substr(2):", str.substr(2));
18 console.log("substr(2,3):", str.substr(2, 3));
19 console.log("substr(-2):", str.substr(-2));
20 console.log("substr(-6):", str.substr(-6, 5));
21 //打印:substr(2): c lmx xyz
22 //打印:substr(2,3): c l
23 //打印:substr(-2): yz
24 //打印:substr(-6): mx xy
(责任编辑:最模板) |

织梦蓝色轴承机械五金模
人气:524
Blanco服装饰品英文综合模
人气:1513
ecshop礼品网模板
人气:1750
ecshop玩具礼品商城模板
人气:552
钻石珠宝商城源码整站|
人气:1989
ecshop免费模板之仿时尚起
人气:4616