日期:2014-05-18  浏览次数:20661 次

在一段文本中有多个地址,要将这些地址全部替换成其他地址怎么做?
我写了一个替换函数String   ss   =     String   replace(parentStr,ch,rep);
ss:替换后的文本
parentStr:要替换的文本
ch:要替换的字符串
rep:替换的字符串
现在我要替换文本里的多个字符串,可能要用递归,但是怎么测试都不行程序发上来,大家看看:
public       String       replace(String       parentStr,String       ch,String       rep)       {          
    int       i       =       parentStr.indexOf(ch);          
    StringBuffer       sb       =       new       StringBuffer();          
    if       (i       ==       -1)          
    return       parentStr;          
    sb.append(parentStr.substring(0,i)       +       rep);          
    if       (i+ch.length()       <       parentStr.length())          
    sb.append(replace(parentStr.substring(i+ch.length(),parentStr.length()),ch,rep));          
    return       sb.toString();          
}
public   String   replaceAllHtml(String   parentStr,String[]   ch,String[]   rep){
String   ss= " ";
if(count==0){
return   parentStr;
}else{
ss   =   this.replace(replace(parentStr,ch[count],rep[count]),ch[count],rep[count]);
count--;


}

return   ss;
}

------解决方案--------------------
String ss = parentStr.replaceAll(ch,rep);
这样就行了
------解决方案--------------------
这种情况下不用正则表达式真是白瞎这门技术了
------解决方案--------------------
楼上的都说了。

------解决方案--------------------
liaohaiying(大菜)
不愧是星星级,7点不到就来答问题了!
------解决方案--------------------
顶星星,hehe ,