那肯定不行了,因为replace替换的是字符,不是字符串,
可以自己写一个函数
 /** 把一个字符中的某些字符串替换为另外的字符串
   *
   * @param sString 传进的字符串字符串
   * @param sOld 串中要替换的字符串
   * @param sNew 串中要替换成的新的字符串
   * @return 替换完成后要返回的字符串
   */
  public static String replaceChars(String sString,String sOld,String sNew) {
   try {
     for (int i = 0; i <=sString.length()-sOld.length(); i ++) {
       if (sString.substring(i, i + sOld.length()).equals(sOld)) {
         sString = sString.substring(0, i) + sNew + sString.substring(i + sOld.length());
         i = i + sNew.length()-1;
       }
     }
     return sString;
   } catch(Exception ex) {
    ex.printStackTrace();
     return sString;
   }
  }

解决方案 »

  1.   

    如果用jdk1.4就用str.replaceAll("\n","<br>");
      

  2.   

    这是转换成要在页面上显示的一个类。请参考package common;
    import java.io.*;public class CodeFilter{
      public CodeFilter() {}
      public static String change(String s) {
      s = toHtml(s);
      return s;
      } //特殊字符转为Html
    public static String toHtml(String s) {
        s = Replace(s,"&","&amp;");
        s = Replace(s,"<","&lt;");
        s = Replace(s,">","&gt;");
        s = Replace(s,"\t","    ");
        s = Replace(s,"\r\n","\n");
        s = Replace(s,"\n","<br>");
        s = Replace(s,"  "," &nbsp;");
        s = Replace(s,"'","&#39;");
        s = Replace(s,"\\","&#92;");
        return s;
        }
    //逆
        public static String unHtml(String s){
    s = Replace(s,"<br>","\n");
    s = Replace(s,"&nbsp;"," ");
    return s;
       }
     
      //Replace
       public static String Replace(String source,String oldString,String newString) {
        if(source == null) return null;
        StringBuffer output = new StringBuffer();
        int lengOfsource = source.length();
        int lengOfold = oldString.length();
        int posStart = 0;
        int pos;
        while((pos = source.indexOf(oldString,posStart)) >= 0) {
          output.append(source.substring(posStart,pos));
          output.append(newString);
          posStart = pos + lengOfold;
        }
        if(posStart < lengOfsource) {
          output.append(source.substring(posStart));
        }
        return output.toString();
      }}