方法一:用charat取出截取部分:
string.prototype.mysubstring=function(beginindex,endindex){ var str=this, newarr=[]; if(!endindex){ endindex=str.length; } for(var i=beginindex;i
string.prototype.mysubstring=function(beginindex,endindex){ var str=this, strarr=str.split(); if(!endindex){ endindex=str.length; } return strarr.slice(beginindex,endindex).join();}//testconsole.log(hello world!.mysubstring(3));//lo world!console.log(hello world!.mysubstring(3,7));//lo w
方法三:取出头尾部分,然后用replace去掉多余部分,适用于beginindex较小,字符串长度-endindex较小的情况:
string.prototype.mysubstring=function(beginindex,endindex){ var str=this, beginarr=[], endarr=[]; if(!endindex){ endindex=str.length; } for(var i=0;i