with String, you can only replace one char with another char:
String replace(char oldChar, char newChar)  with StringBuffer, you have
StringBuffer replace(int start, int end, String str)  guess you might have to write you own, like:// Replace all occurrences of o in str with n,
// or only the first occurrence if all is false.
// replace("aaaa", "aa", "bbb", false) returns "bbbaa"
// replace("aaaa", "aa", "bbb", true)  returns "bbbbbb"
String replace(String str, String o, String n, boolean all) {
    if (str == null || o == null || o.length() == 0 || n == null)
        throw new IllegalArgumentException("null or empty String");
    StringBuffer result = null;
    int oldpos = 0;
    do {
        int pos = str.indexOf(o, oldpos);
        if (pos < 0)
            break;
        if (result == null)
            result = new StringBuffer();
        result.append(str.substring(oldpos, pos));
        result.append(n);
        pos += o.length();
        oldpos = pos;
    } while (all);
    if (oldpos == 0) {
        return str;
    } else {
        result.append(str.substring(oldpos));
        return new String(result);
    }
}

解决方案 »

  1.   

    建立一个Beans为replaceBeans;
    String scr="";
    scr=replaceBeans.replace("aaaa","aa","bbb",true);
    out.println(scr);
    结果出现
    org.apache.jasper.JasperException: Unable to compile class for JSPF:\MyJava\BsDather\bsdatagather\work\localhost_8080\_0002fTableTypes_0002ejspTableTypes_jsp_100.java:110: class bsdatagather.loginBean ?????? replace(java.lang.String, java.lang.String, java.lang.String, boolean)?
                        scr=loginBeanId.replace("aaaa","aa","bbb",true);
                                               ^
      

  2.   

    jdk1.31已经提供了 replace(char oldChar, char newChar) 
              Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.java.lang.String 
    有的