jdk1.4里有一个方法
replaceAll
去看看吧下面是我写的
    /**
     * 字符串替换函数
     * @param sAll String   原来的字符串
     * @param older String  要替换掉的字符串
     * @param newer String  新的字符串
     * @return String       替换后的结果
     */
    public synchronized static  String strReplace(String sAll,String sOld, String sNew){
        int iT=0;
        String sF = null,sH= null;
        //如果新串中包括旧串,不让替多只让替少
        if(sNew.indexOf(sOld)!= -1)
            return sAll;        if(sAll == null || sOld==null ||sNew==null)
            return sAll;
        iT = sAll.indexOf(sOld);
        int i = 0;
        while(iT != -1){
            sF = sAll.substring(0,iT);
            sH= sAll.substring(iT+sOld.length());
            sAll = sF+sNew+sH;
            iT = sAll.indexOf(sOld);
        }
        return sAll;
    }

解决方案 »

  1.   

    大哥,注释写的很清楚了而且现在jdk1。4已经有可以用的函数了
    public String repalceAll(String regex,String replacement)
      

  2.   

    public static String Replace(String p_strSource, String   p_strFrom, String p_strTo){
        String p_strDest = "";
        //get the length of the string
        int l_intFromLen = p_strFrom.length();
        //the position in the string
        int l_intPos;
        while((l_intPos=p_strSource.indexOf(p_strFrom))!=-1){
          p_strDest = p_strDest + p_strSource.substring(0,l_intPos);
          p_strDest = p_strDest + p_strTo;
          p_strSource = p_strSource.substring(l_intPos+l_intFromLen);
        }
        p_strDest = p_strDest + p_strSource;
        return p_strDest;
      }