代码改变世界

slice 和substring()

2011-08-05 16:34  鹏雕  阅读(209)  评论(0编辑  收藏  举报

var myStr = new String("hello world");

document.write(myStr.slice(3) + "<br/>");//lo world
document.write(myStr.substring(3), "<br />"); //lo world
document.write(myStr.slice(3, 7), "<br />");   //output lo w
document.write(myStr.substring(3, 7), "<br/>"); //output lo w
document.write(myStr.slice(-3), "<br/>"); //output rld
document.write(myStr.substring(-3), "<br/>"); //output hello world   负数相当于0
document.write(myStr.slice(3, -4), "<br/>"); //相当于slice37output lo w
document.write(myStr.substring(3, -4), "<br />"); //output hel 相当于substring03)较小的数字放在第一个参数