String类有一个函数replaceAll("","")

解决方案 »

  1.   

    很遗憾,在我的JDK库里没有
    String.replaceAll("", "");
    唯一有的还只是
    Stringreplace('', '');所以只好自己写一个,好在不是很麻烦。public static String replace(String source, String oldStr, String newStr) {
        String result = "";
        
        int k = 0;
        for (int i = 0; i < source.length(); ) {
            k = source.indexOf(oldStr, i);
            if (k > -1) {
                result += source.substring(i, k);
                result += newStr;
                i = k + oldStr.length();
            }
            else {
                result += source.substring(i);
                break;
            }
        }    return result;
    }
      

  2.   

    To zhishao(zhi) :   你的方法又如何使用呢? 如果我需要将字符串中的"(双引号)替换为\"(反斜线+双引号), 则调用方法时的后两个参数应该如何写, 
    是否可以这样replace(str, "\"", "\\\"")
    是吗? 可是不行!
      

  3.   

    /**
         * 使用rep替换s中的find。
         * StringTokenizer的实现。
         * @param s 源字符串
         * @param find 匹配子串
         * @param rep 替换字符串
         * @return 处理后的源字符串
         */
        public final static String replace( String s, String find, String rep )
                                    throws Exception
        {
            RE re = new RE( find );        return re.subst( s, rep );
        }    /**
         * 使用rep替换s中的find。
         * StringTokenizer的实现。
         * @param s 源字符串
         * @param find 匹配子串
         * @param rep 替换字符串
         * @return 处理后的源字符串
         */
        public final static String replaceString( String s, String find,
                                                  String rep )
        {
            String out;
            int targetLength = s.length();
            int replaceLength = rep.length();
            int matchLength = find.length();
            StringTokenizer st = new StringTokenizer( s, find );
            int firstIndex = s.indexOf( find );        if ( firstIndex < 0 )
            {
                // 无匹配
                out = s;            return out;
            }        if ( firstIndex > 0 )
            {
                out = "";
            }
            else
            {
                // 以要替换的字符串开头
                out = rep;
            }        while ( st.hasMoreTokens() )
            {
                out += st.nextToken() + rep;
            }        out = out.substring( 0, out.length() - replaceLength );        int lastIndex = s.lastIndexOf( find );        // 以要替换的字符串结尾
            if ( lastIndex == ( targetLength - matchLength ) )
            {
                out = out + rep;
            }        return out;
        }    /**
         * @author beh
         *
         * @ Modified by : Greg Karbett
         *
         * Replaces one substring with another within a main string.
         *
         * @param s Main string.
         * @param find The substring to find.
         * @param rep Replaces the find substring.
         * @return The new string.
         */
        public static String replaceStringBuffer( String s, String find,
                                                  String rep )
        {
            return replace( new StringBuffer( s ), find, rep ).toString();
        }
        /**
         * @author beh
         *
         * @ Modified by : Greg Karbett
         *
         * Replaces one substring with another within a main string.
         *
         * @param sb Main string. (StringBuffer)
         * @param find The substring to find.
         * @param rep Replaces the find substring.
         * @return The new string. (StringBuffer)
         */
        private static StringBuffer replace( StringBuffer sb, String find,
                                             String rep )
        {
            StringBuffer buf = new StringBuffer( sb.toString() );
            int startIndex = 0;
            int stringLocation = 0;
            boolean done = false;        // Halts if they is not need to do any replacements
            if ( ! find.equals( rep ) )
            {
                String s = buf.toString();            // Continues while more substring(s) (find) exist
                while ( ! done )
                {
                    // Grab the position of the substring (find)
                    if ( ( stringLocation = s.indexOf( find, startIndex ) ) >= 0 )
                    {
                        // Replace "find" with "rep"
                        buf.delete( stringLocation, stringLocation +
                                    find.length() );
                        buf.insert( stringLocation, rep );
                        startIndex = stringLocation + rep.length();
                        s = buf.toString();
                    }
                    else
                    {
                        done = true;
                    }
                }
            }        return buf;
        }
      

  4.   

    程序没问题啊,我测试过了。
    taozabc(Pluto)说的方法可行啊,没问题。
    字符串"\\\""不就是等于实际上的\"吗?
    我也测试通过了。
      

  5.   

    可以吗?我咋还是不行?我用以下:
    public class ReplaceChar
    {
       public static void main(String[] args)   {
          System.out.println(replace(args[0], "\"", "\\\""));
       }
    }使用命令: java ReplaceChar tttase"sdfae得到的结果是:tttasesdfae
    把"丢掉了。
      

  6.   

    使用命令的问题
    java ReplaceChar tttase"sdfae
    这样运行的话,args[0]本身就等于tttasesdfae
    若要args[0]为tttase"sdfae,则需要用命令
    java ReplaceChar tttase\"sdfae
      

  7.   

    faint
    运行命令java ReplaceChar tttase"sdfae
    此时args[0]本身就是tttasesdfae,如何能转换成功?
    要想args[0]是tttase"sdfae,运行命令必须是
    java ReplaceChar tttase\"sdfae才行
    另外:String的replaceAll方法是jdk1.4才带有的
      

  8.   

    to  alphazhao(绿色咖啡) :小弟愚昧,为何“运行命令java ReplaceChar tttase"sdfae
    此时args[0]本身就是tttasesdfae”??谢谢1!!!
      

  9.   

    用字符分析器StringTokenizer分解,在合成不就完了麻