尊重作者原创,未经作者允许不得转载,作者:xtfnpgy,原文地址:
https://blog.csdn.net/xtfnpgy/article/details/80311510
/*字符串截取方法*/
var s="abc_def[ghi]jk[i]";
var temp;function CopyFromStr(str_source,str_key, bl_containKey)
{
if(str_source.indexOf(str_key)==-1)
{ return "";}
else
{
if(bl_containKey)
{ return str_source.substr(str_source.indexOf(str_key), str_source.length - str_source.indexOf(str_key) + 1);}
else
{ return str_source.substr(str_source.indexOf(str_key)+str_key.length, str_source.length - str_source.indexOf(str_key)+str_key.length + 1);}
}
}
function CopyToStr(str_source,str_key,bl_containKey)
{
if(str_source.indexOf(str_key)==-1)
{ return "";}
else
{
if(bl_containKey)
{ return str_source.substr(0, str_source.indexOf(str_key) + str_key.length);}
else
{ return str_source.substr(0, str_source.indexOf(str_key) );}
}
}
function CopyStr(str_source,str_start,str_end,bl_containStartEnd)
{
var i,j;
var tmp;
tmp = str_source;
i = tmp.indexOf(str_start);
if(i==-1) {return "";}
tmp = CopyFromStr(tmp,str_start,false);
j = tmp.indexOf(str_end);
if(j==-1) {return "";}
if(bl_containStartEnd)
{return str_start + tmp.substr(0, j) + str_end;}
else
{return tmp.substr(0,j);}
}
function CopyStrEx(str_source, str_key, str_start,str_end,bl_containStartEnd)
{
var tmp = str_source;
if((tmp.indexOf(str_key)==-1) ||
(tmp.indexOf(str_start)==-1) ||
(tmp.indexOf(str_end)==-1) )
{return "aaa";}
else
{
tmp = CopyFromStr(tmp,str_key,false);
if(bl_containStartEnd)
{return CopyStr(tmp,str_start,str_end,true);}
else
{return CopyStr(tmp,str_start,str_end,false);}
}
}
function RightFromStr(str_source,str_key,bl_containStartEnd)
{
if(str_source.indexOf(str_key)==-1)
{ return "";}
else
{
while(str_source.indexOf(str_key)>-1)
{str_source = CopyFromStr(str_source,str_key,false);}
if(bl_containKey)
{ return str_key+str_source;}
else
{ return str_source;}
}
}
function RightFromStr(str_source,str_key,bl_containStartEnd)
{
if(str_source.indexOf(str_key)==-1)
{ return "";}
else
{
while(str_source.indexOf(str_key)>-1)
{str_source = CopyFromStr(str_source,str_key,false);}
if(bl_containStartEnd)
{ return str_key+str_source;}
else
{ return str_source;}
}
}
/*调用方法:*/
temp=CopyFromStr(s, '[gh', false);alert(temp);
temp=CopyToStr(s, '[gh', false);alert(temp);
temp=CopyStr(s, '_', '[gh', false);alert(temp);
temp=CopyStrEx(s, '_', '[', ']', false);alert(temp);
temp=RightFromStr(s, '[', false);alert(temp);
尊重作者原创,未经作者允许不得转载,作者:xtfnpgy,原文地址:
https://blog.csdn.net/xtfnpgy/article/details/80311510