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());
  }

解决方案 »

  1.   

    在类string中有现成的方法可以调用,自己看看吧
      

  2.   

    好象String中只有替换单个字符,没有替换字符串的现成方法,还是的自己写。
      

  3.   

    同意skyyoung & lmy2000, 呵呵 ...
    --
    http://www.csdn.net/expert/topic/77/77231.shtm
      

  4.   

    还有一种办法,用stringbuffer转。
    StringBuffer类的replace可以实现字符串的替换.
      

  5.   

    同意yangzi的看法,这才是最快的方法,不过StringBuffer中的replace是JDK1.2之后才有的,所以我写的一个程序中使用的方法大概如下:1   public String replaceSingleTag(String strToProcess, String strFrom, String strTo) {
    2      String tempStr = strToProcess;
    3  //    StringBuffer temp = new StringBuffer(tempStr);
    4      int first=0, last=0;5     while((first = tempStr.indexOf(strFrom))!= -1) {
    6       last = first + strFrom.length();
    7       StringBuffer temp = new StringBuffer("");
    8       temp.append(tempStr.substring(0, first));
    9       temp.append(strTo);
    10       temp.append(tempStr.substring(last));
    11 //      temp = temp.replace(first, last, strTo);
    12      tempStr = new String(temp);
    13     }
    14    return tempStr;
    15   }如果你用的JDK是1.2以下的话就不用改了,
    要是用的1.2 就把7-10句注释掉,把3和11前的注释去掉。
    这个函数的作用是将strToProces中所有的strFrom 替代成strTo,不过有几个小毛病,不知各位大虾看过之后能否指出。:)
      

  6.   

    呵呵 ... 我觉得这样才象论坛 :)
    谢谢yangzi和luodi的指导 :)
      

  7.   

    555555 ... 这才想起来, 用JTest测过, 发现若干应该用StringBuffer结果用String的地方, 错误是改了, 但是习惯还没改 :(
      

  8.   

     /**替换字符的通用方法             源字串,要替换源字串,替换为的目的字串*/
      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;
      }
      

  9.   

    也可以试试这个:
    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;
      }
    }