php string slice,substring()与str.slice()区别

本文通过测试代码展示了JavaScript中字符串方法slice、substring和substr处理负数参数时的不同行为。当参数为负数时,slice将其与字符串长度相加,substr同样使用此规则,而substring将负数参数转换为0。这些差异在处理字符串切片时需要注意。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当接收的参数是负数时,slice会将它字符串的长度与对应的负数相加,结果作为参数;substr则仅仅是将第一个参数与字符串长度相加后的结果作为第一个参数;substring则干脆将负参数都直接转换为0。测试代码如下:

var test = 'hello world';

document.write(test.slice(-3)+'
');         //rld

document.write(test.substring(-3)+'
');     //hello world

document.write(test.substr(-3)+'
');        //rld

document.write(test.slice(3,-4)+'
');       //lo w

document.write(test.substring(1,0)+'
');   //hel

document.write(test.substr(3,-4)+'
');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值