代码改变世界

【工具类】通配符替换

2020-06-02 10:08  HA_Tinker  阅读(310)  评论(0编辑  收藏  举报

 

由于日常研发,有时候需要配置相应的模板,并且通过内容进行快速替换,因此写了一个统一的工具类。

可以根据需要把params缓存Map,进行Key查找替换。

   public static void main(String[] args) throws Exception{
//通过开闭符,进行替换 String text
= "{\"ID\":\"1${{a}23$\"}"; String[] args1 = {"11","22","33"}; String result = JsonStringParse("${", "}", text, args1); System.out.println(result); }

 

public static String JsonStringParse(String openToken,String closeToken,String jsonText,Object... params){
        //判断是否需要替换
        if(params == null || params.length <= 0
                || jsonText == null || jsonText.isEmpty()
                || openToken == null ||openToken.isEmpty()
                || closeToken == null || closeToken.isEmpty()){
            return jsonText;
        }

        final StringBuilder builder = new StringBuilder("");

        char[] src = jsonText.toCharArray();
        char[] open = openToken.toCharArray();
        char[] close = closeToken.toCharArray();

        int paramsIndex = 0;
        int offset = 0;
        boolean openFlag = true;

        //循环查找开闭字符
        for(int i = 0; i < src.length; i++){

            if(openFlag){
                //serch OpenToken
                if(src[i] == open[0]){

                    if(open.length > 1){
                        if(src.length - i < open.length|| !compareChars(src,i,open)){
                            continue;
                        }
                    }

                    builder.append(src, offset, i - offset);
                    offset = i;
                    i = i + open.length - 1;
                    openFlag = false;
                }
            }else{
                //serch CloseToken
                if(src[i] == close[0]){
                    if(close.length > 1){
                        if(src.length - i < close.length|| !compareChars(src,i,close)){
                            continue;
                        }
                    }

                    if(paramsIndex <= params.length - 1){
                        if(params[paramsIndex] != null){
                            builder.append(params[paramsIndex]);
                        }else {
                            builder.append(src, offset, i - offset).append(closeToken);
                        }
                    }else{
                        builder.append(src, offset, i - offset).append(closeToken);
                        break;
                    }

                    offset = i + close.length;
                    i = i + close.length - 1;
                    openFlag = true;
                    paramsIndex++;
                }
            }
        }

        if (offset < src.length) {
            builder.append(src, offset, src.length - offset);
        }

        return builder.toString();

    }

 

public static boolean compareChars(char[] src,int begin, char[] target) {
        for (int i = 0; i < target.length;i++){
            if(src[begin+i] != target[i]){
                return false;
            }
        }
        return true;
    }