包含替换内容的相近字符的replace问题

一种是全量匹配,被替换的字符在该字符中只会存在一次

public class StringReplaseUtil extends org.springframework.util.StringUtils{

    public  static  void main(String[] agrs){
        String s = "AAASELECT AAA,AAA_B,AA_C,AA FROM DUAL AA_ AAA_";
        System.out.println(replace(s,"AAA","a"));
    }

    public static String replace(String inString, String oldPattern, String newPattern) {
        if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) {
            return inString;
        }
        int index = inString.indexOf(oldPattern);
        if (index == -1) {
            // no occurrence -> can return input as-is
            return inString;
        }

        int capacity = inString.length();
        if (newPattern.length() > oldPattern.length()) {
            capacity += 16;
        }
        StringBuilder sb = new StringBuilder(capacity);
        int pos = 0;
        int patLen = oldPattern.length();
        while (index >= 0) {
            String jz = inString.substring(index+patLen, index+patLen+1);
            if(",".equals(jz) || " ".equals(jz)){
                sb.append(inString.substring(pos, index));
                sb.append(newPattern);
            }else{
                if(index > pos ){
                    sb.append(inString.substring(pos,index));
                }else{
                    sb.append(inString.substring(pos,index+patLen));
                }
            }
            pos = index + patLen;
            index = inString.indexOf(oldPattern, pos);
            if(index == -1){
                sb.append(inString.substring(pos-patLen));
            }
        }
        return sb.toString();
    }
}

另外一种是被替换的字符在该字符串中会出现多次

public class StringReplaseUtil extends org.springframework.util.StringUtils{

    public  static  void main(String[] agrs){
        String s = "AAASELECT AAA,AAA_B,AA_C,AA,AAA AAAFROM DUAL AA_ AAA_";
        System.out.println(replace(s,"AAA","a"));
    }

    public static String replace(String inString, String oldPattern, String newPattern) {
        if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) {
            return inString;
        }
        int index = inString.indexOf(oldPattern);
        if (index == -1) {
            // no occurrence -> can return input as-is
            return inString;
        }

        int capacity = inString.length();
        if (newPattern.length() > oldPattern.length()) {
            capacity += 16;
        }
        StringBuilder sb = new StringBuilder(capacity);
        int pos = 0;
        int patLen = oldPattern.length();
        while (index >= 0) {
            String jz = inString.substring(index+patLen, index+patLen+1);
            if(",".equals(jz) || " ".equals(jz)){
                sb.append(inString.substring(pos, index));
                sb.append(newPattern);
            }else{
                sb.append(inString.substring(pos,index+patLen));
            }
            pos = index + patLen;
            index = inString.indexOf(oldPattern, pos);
            if(index == -1){
                sb.append(inString.substring(pos));
            }
        }
        return sb.toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值