三个函数均不会修改原数组或字符串
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;">
缺省第二个参数默认返回起始下标(包含)到字符串结尾的子串;
函数自动将负参数转换为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>