substr(),substring,slice()的用法


          三个函数均不会修改原数组或字符串

         substr():返回字符串起始下标(包含)到指定长度的字符串,仅作用于字符串;
                       缺省第二个参数时默认返回从起始下标到字符串结尾的子串;
                       参数为负数时逆向考虑,逆向初始下标为-1;
                       第二个参数为负数时返回空字符串;

        var strs = ["this","is","a","array!"];
	var str = "this is a string!";
	$("p").eq(0).text(str.substr(1,5));		//his i
	$("p").eq(1).text(str.substr(1));		//his is a string!
	$("p").eq(2).text(str.substr(-3));		//ng!
	$("p").eq(3).text(str.substr(-9,4));		//a st
	$("p").eq(4).text(str.substr(-9));		//a string!
	$("p").eq(5).text(str.substr(-9,-2));		//""</span><span style="color:#FF0000;">



            substring():返回字符串起始下标(包含)到终止下标(不包含)的字符串,仅作用于字符串;
                        缺省第二个参数默认返回起始下标(包含)到字符串结尾的子串;
                        函数自动将负参数转换为0;
                        函数总是返回较小下标到较大下标之间的字符串,而忽略其顺序;
        $("p").eq(6).text(str.substring(1,2));		//h
	$("p").eq(7).text(str.substring(1));		//his is a string!
	$("p").eq(8).text(str.substring(-3));		//this is a string!
	$("p").eq(9).text(str.substring(2,1));		//h
	$("p").eq(10).text(str.substring(-3,-1));	//""
	$("p").eq(11).text(str.substring(-3,12));	//this is a st</span>

          


        slice():返回起始下标(包含)到终止下标(不包含)之间的子串或子数组;

                    缺省第二个参数时默认到数组或字符串结尾;
                    参数为负数时逆向考虑,逆向初始下标为-1

        $("p").eq(12).text(strs.slice(1,3));		//is,a
	$("p").eq(13).text((strs.slice(1,3))[1]);	//a
	$("p").eq(14).text(strs.slice(1));		//is,a,array!
	$("p").eq(15).text(strs.slice(-1));		//array!
	$("p").eq(16).text(strs.slice(-3,-2));		//is
	$("p").eq(17).text(strs.slice(3,1));		//""
	$("p").eq(18).text(strs.slice(-3,3));		//is,a
			
	$("p").eq(19).text(str.slice(1,2));		//h
	$("p").eq(20).text(str.slice(1));		//his is a string!
	$("p").eq(21).text(str.slice(-3));	        //ng!
	$("p").eq(22).text(str.slice(-5,-2));		//rin</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值