JS中string方法中常用方法之六:String.prototype.substring()

String.prototype.substring()返回字符串两个索引之间(或到字符串末尾)的子串。
语法:str.substring(indexStart[, indexEnd])

详解:
substring 提取从 indexStart 到 indexEnd(不包括)之间的字符。特别地:
如果 indexStart 等于 indexEnd,substring 返回一个空字符串。
如果省略 indexEnd,substring 提取字符一直到字符串末尾。
如果任一参数小于 0 或为 NaN,则被当作 0。
如果任一参数大于 stringName.length,则被当作 stringName.length。
如果 indexStart 大于 indexEnd,则 substring 的执行效果就像两个参数调换了一样。例如,str.substring(1, 0) == str.substring(0, 1)。



//举例如下
var anyString = "Mozilla";


// 输出 "Moz"
console.log(anyString.substring(0,3));
console.log(anyString.substring(3,0));
console.log(anyString.substring(3,-3));
console.log(anyString.substring(3,NaN));
console.log(anyString.substring(-2,3));
console.log(anyString.substring(NaN,3));


// 输出 ""
console.log(anyString.substring(4,4));




// 输出 "zill"
console.log(anyString.substring(2));


// 输出 "Mozilla"
console.log(anyString.substring(0,7));
console.log(anyString.substring(-2,7));
console.log(anyString.substring(NaN,7));
console.log(anyString.substring(0,10));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值