S4 += replaceString这种连加的操作能不用StringBuffer吗

解决方案 »

  1.   

    我那个StringReplace方法应付较小字符串的替换应是没问题的,用StringBuffer替换应该怎么写呢?
      

  2.   

    为什么要自己写替换方法呢?String类的replaceAll不能用吗?
      

  3.   

    @see org.apache.commons.lang.StringUtils#replace(String,String,String)
      

  4.   

    /**
     * @param str 原字符串
     * @param src 要替换的标签
     * @param dest 替换标签用的字符串
     */
    public static String stringReplace(String str, String src,String dest){
        StringTokenizer st = new StringTokenizer(str,src);
        StringBuffer buff = new StringBuffer(str.length * 4);// 缓冲大小可以根据实际情况自定义    if(str.startsWith(src))
            buff.append(dest);    while(st.hasMoreElements()){
            buff.append(st.nextElement());
            buff.append(dest);  
        }
        if(!str.endsWith(src))
            buff.delete(buff.length()-dest.length(),buff.length());
        return buff.toString();
    }
      

  5.   

    楼上的办法试了,抛出例常,报错,具体如下:
    javax.servlet.ServletException
    root cause java.lang.OutOfMemoryError是缓冲大小的问题吗?我已经改成很大了
    另外haroyy(天平)说的replaceAll我怎么没在JDK中找到这个方法呢?
    xiaohaiz(城里的老土,两眼依然通红!) 说的
    @see org.apache.commons.lang.StringUtils#replace(String,String,String)是在那个包中的?
      

  6.   

    replaceAll(String regx,String replacement)
    String 的一个方法
      

  7.   

    看看我这个方法,以前贴过的,反响很好    /**
         * 字符串内容替换, 根据不同版本的 JDK 做不同的处理
         * @param strSource 源字符串
         * @param strLookfor 查找的字符串
         * @param strSubstitute 替换成的字符串
         * @return 异常返回 null ,参数不合法返回 "" ,成功返回已替换的字符串
         */
        public static String replaceAll(String strSource, String strLookfor,
                String strSubstitute) {
            if (strSource == null || strSource.equals("")) {
                return "";
            }
            if (strLookfor == null || strLookfor.equals("")) {
                return strSource;
            }
            if (strSubstitute == null) {
                strSubstitute = "";
            }
            StringBuffer sb = new StringBuffer("");
            int begin = 0;
            int end = strSource.indexOf(strLookfor);
            while (end >= 0) {
                sb.append(strSource.substring(begin, end));
                sb.append(strSubstitute);
                begin = end + strLookfor.length();
                end = strSource.indexOf(strLookfor, begin);
            }
            sb.append(strSource.substring(begin, strSource.length()));
            return new String(sb);
        }
      

  8.   

    replaceAll public String replaceAll(String regex,
                             String replacement)Replaces each substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl) Parameters:regex - the regular expression to which this string is to be matched Returns:The resulting String Throws: PatternSyntaxException - if the regular expression's syntax is invalid NullPointerException - if regex is nullSince:1.4See Also:Pattern
      

  9.   

    replaceAll public String replaceAll(String regex,
                             String replacement)
    Replaces each substring of this string that matches the given regular expression with the given replacement. 
    An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
     Pattern.compile(regex).matcher(str).replaceAll(repl) 
    Parameters:
    regex - the regular expression to which this string is to be matched
    Returns:The resulting String 
    Throws: PatternSyntaxException - if the regular expression's syntax is invalid NullPointerException - if regex is null
    Since:1.4
    See Also:PatternJDK文档,从JDK1.4就开始有的String的一个方法
      

  10.   

    天,我还在看JDK1.3的文档
    谢谢大家!