jdk1.4中有个很好用的函数,replaceAll(),你可以用它来替换所得到的所有字符,比如replaceAll("\n","<br>")

解决方案 »

  1.   

    帮我贴个与replaceAll()功能一样的函数好吗?..
      

  2.   

    public String replace(String line,String ch,String rep)
    {
    int i=line.indexOf(ch);
    StringBuffer sb=new StringBuffer();
    if (i==-1)
    return line;
    sb.append(line.substring(0,i)+rep);
    if(i+ch.length()<line.length())
    sb.append(replace(line.substring(i+ch.length(),line.length()),ch,rep));
    return sb.toString();
    }String test='123'
    test=replace(test,"'","&quot;");//'->"在注册时可以用于特殊字符的替换
      

  3.   

    public static final String replace(String s, String s1, String s2)
        {
            if(s == null || s1 == null || s2 == null)
                return "";
            int i = 0;
            boolean flag = false;
            if((i = s.indexOf(s1, i)) >= 0)
            {
                char ac[] = s.toCharArray();
                char ac1[] = s2.toCharArray();
                int k = s1.length();
                StringBuffer stringbuffer = new StringBuffer(ac.length);
                stringbuffer.append(ac, 0, i).append(ac1);
                i += k;
                int j;
                for(j = i; (i = s.indexOf(s1, i)) > 0; j = i)
                {
                    stringbuffer.append(ac, j, i - j).append(ac1);
                    i += k;
                }            stringbuffer.append(ac, j, ac.length - j);
                return stringbuffer.toString();
            } else
            {
                return s;
            }
        } public static final String htmlToCode(String s)
        {
            if(s == null)
            {
                return "";
            } else
            {
                s = replace(s, "<p>", "\n\n");
                s = replace(s, "<br>", "\r\n");
                s = replace(s, "&nbsp;&nbsp;&nbsp;&nbsp;", "\t");
                s = replace(s, "&nbsp;", " ");
                s = replace(s, "&lt;", "<");
                s = replace(s, "&gt;", ">");
                s = replace(s, "&amp;", "&");
                return s;
            }
        }  public static final String htmlEncode(String s)
        {
            if(s == null)
                return "";
            char ac[] = s.toCharArray();
            StringBuffer stringbuffer = new StringBuffer(ac.length);
            for(int i = 0; i < ac.length; i++)
            {
                char c = ac[i];
                if(c == '<')
                    stringbuffer.append("&lt;");
                else
                if(c == '>')
                    stringbuffer.append("&gt;");
                else
                if(c == '"')
                    stringbuffer.append("&quot;");
                else
                if(c == '&')
                    stringbuffer.append("&amp;");
                else
                    stringbuffer.append(c);
            }        return stringbuffer.toString();
        }