替换“dsBBfvAAxcvAAasxcBBB" 里的AA和BB为空串的方法,真实字符串可能很长,求最高效方法

如果为了一般的替换方法,用正则的替换方法就行了。
正则的替换经过了测试和验证。在保持高效同时,也保证了正确。
不过,正则表达式不会太聪明的,在性能上,比不过你自己手写的。但你手写的保证不了质量。
如果这个表达式替换用手写与正则的比较,大概是1:2,数据量大时,1:3.
当然,如果不是为了特别快的性能,最好用正则表达式。
Java code
    public static void main(String[] args) {
        String str = "xx9iijfdsfAAkkkBBkkkkkkkAABBsfjDDfsdfhBBfhAsdhhAAAAdshfdxAA9AAdsfAAkBBkkBBkBBkkkkkkksfjsdjAAfsBBdfhBBdshAAfhsBBdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkBBkkksfjsdjfsdfhdshfhsBBBBBfdshfdx9iiBBAAAAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfAAAdsBBhBBiijfdsfAAkkkBBkkkkkkkkkkkBBBdfhdSSBBfhAAAhfsdfdBBBjfdsfAAkkkBBAAkkkkkkksfjsdjBBhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdx9iijfdsfAAkkkBBkkkkkkkkkkksfjsdjfsdfhdshfhsdhhfsdfdshfdfhsdfhAAABBkkkBB";
        str=str+str+str;
        str=str+str+str;
        long begintime = System.currentTimeMillis();
        String replaceStr = str.replaceAll("A{2}|B{2}", "  ");
        long endtime = System.currentTimeMillis();
        //System.out.println(replaceStr);
        System.out.println();
        System.out.println("正则用的时间:" + (endtime - begintime));
        begintime = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        char lastC = str.charAt(0);
        char nowC = '1';
        for (int i = 1; i < str.length(); i++) {
            nowC = str.charAt(i);
            if ((nowC == 'A' || nowC == 'B') && nowC == lastC) {
                /* 替换 */
                sb.append("  ");
                if (i+1< str.length()) {
                    lastC = str.charAt(i + 1);
                    i++;
                    if (i == str.length() - 1) {
                        sb.append(lastC);
                    }
                }
            } else {
                sb.append(lastC);
                lastC = nowC;
               
                if (i == str.length() - 1) {
                    sb.append(lastC);
                }
            }
        }
        endtime = System.currentTimeMillis();
        System.out.println(sb.toString().equals(replaceStr));
        System.out.println();
        System.out.println("手写用的时间:" + (endtime - begintime));
    }

你可能感兴趣的:(java,字符串,休闲,空串,真实字符串)