(1).public static String StrReplace(String rStr, String rFix, String rRep)
{
int l = 0;
String gRtnStr = rStr;
do
{
l = rStr.indexOf(rFix,l);
if(l == -1) break;
gRtnStr = rStr.substring(0,l) + rRep + rStr.substring(l + rFix.length());
l += rRep.length();
rStr = gRtnStr;
}while(true);
return gRtnStr.substring(0, gRtnStr.length());
}
說明:rStr 源字符串
rFix 要查找替換的字符串
rRep 替換成的字符串
(2)/** 源字串,要替換源字串,替換為的目的字串*/
public static String replace1(String s,String org,String ob)
{
String newString="";
int first=0;
while (s.indexOf(org)!=-1)
{
first=s.indexOf(org);
if (first!=s.length() )
{
newString=newString+s.substring(0,first)+ob ;
s=s.substring(first+org.length() ,s.length() ) ;
}
}
newString=newString+s;
return newString;
}
(3)public class strReplace{
public static String Replace(String strReplaced, String oldStr, String newStr){
int pos=0;
int findPos;
while((findPos=strReplaced.indexOf(oldStr,pos))!=-1){
strReplaced=strReplaced.substring(0,findPos)+newStr+strReplaced.substring(findPos+oldStr.length());
findPos+=newStr.length();
}
return strReplaced;
}
}
聯(lián)系客服