2019-12-17-自定义文本处理工具

我的需求

因写文档时逐条替换耗时,同时常用json字符串。

使用步骤

第一步,点击链接进入

打开链接
该工具已移动到 更多 -> Mroe -> 控制台 -> 基本工具 -> EQ-Tool

第二步,在导航栏中选择"字符串操作"

点击选项

第三步,选择模式,执行工具

直接执行,查看提示

文本压缩
文本压缩

批量替换
批量替换

转为json
转为json

转为字符串
转为字符串

相关代码

主要代码
    @RequestMapping("/***")
    @ResponseBody
    public String strAndJsonTransTool(String transmodel, String transtxt,
                                      String spA, String spB){
        //定义结果str
        String res = "转换失败!";
        //判断处理类型
        if ("txtReplace".equals(transmodel)){
            //文本批量替换
            try{
                res = getReplacement(transtxt, spA, spB);
            }catch (Exception e){
                return "替换失败,请检查格式!\n格式样例如下:(将“测试”“文本”替换为“样例”“内容”(“:”“;”也可在spA,spB中输入))\n" +
                        "测试:样例;文本:内容;######测试中的文本";
            }
        }
        if ("strToJson".equals(transmodel)){
            //转换成json字符串
            try{
                res = transJsonString(transtxt, spA, spB);
            }catch (Exception e){
                return "转换Json字符串失败,请检查格式!\n格式样例如下:(“:”“;”也可在spA,spB中输入)\nname:小李;age:18;";
            }
        }
        if ("jsonToStr".equals(transmodel)){
            //转换成普通字符串
            // 逗号替换为spB,冒号替换为spA
            transtxt = transtxt.replaceAll(":",spA);
            transtxt = transtxt.replaceAll(",",spB);
            transtxt = compressStr(transtxt);//文本压缩,去掉换行、空格、制表位
            transtxt = transtxt.replaceAll("\"","");//去掉引号
            try{
                //去掉首尾的大括号
                transtxt = transtxt.substring(1, transtxt.length() - 1);
                res = transtxt + spB;
            }catch (Exception e){
                return "转换字符串失败,请检查格式!\n格式样例如下:\n" +
                        "{\n" +
                        "\t\"name\": \"小李\",\n" +
                        "\t\"age\": \"18\"\n" +
                        "}";
            }
        }
        if ("compressStr".equals(transmodel)){
            //文本压缩
            res = compressStr(transtxt);//文本压缩,去掉换行和空格等
        }
        return res;
    }
所调用的方法
    private String compressStr(String transtxt) {
        transtxt = transtxt.replaceAll(" ","");//去掉空格
        transtxt = transtxt.replaceAll("\t","");//去掉制表符
        transtxt = transtxt.replaceAll("\n","");//去掉换行
        return transtxt;
    }

    private String getReplacement(String transtxt, String spA, String spB) {
        String res;//根据“######”将前后内容分割
        String[] split = transtxt.split("######");
        String conReplace = split[0];//操作内容
        String conTxt = split[1];//文本内容

        //使用GetKeyValueList类中的方法获取键值List(键:值 -> 替换内容:待替换内容)
        GetKeyValueList getKeyValueList = new GetKeyValueList(conReplace, spA, spB).invoke();
        List<String> keyList = getKeyValueList.getKeyList();
        List<String> valueList = getKeyValueList.getValueList();

        //遍历替换内容keyList
        for (int i = 0; i < keyList.size(); i++){
            //在文本内容中逐条将key替换为value
            conTxt = conTxt.replaceAll(keyList.get(i), valueList.get(i));
        }
        res = conTxt;
        return res;
    }

    private String transJsonString(String transtxt, String spA, String spB) {
        String res;

        //使用GetKeyValueList中的方法获取键值List
        GetKeyValueList getKeyValueList = new GetKeyValueList(transtxt, spA, spB).invoke();
        List<String> keyList = getKeyValueList.getKeyList();
        List<String> valueList = getKeyValueList.getValueList();

        //开始拼接字符串
        res = "{\n";
        //从键值集合中取值
        for (int i = 0; i < keyList.size(); i++){
            res += "\t";
            res += andQuetes(keyList.get(i))+": " + andQuetes(valueList.get(i));
            if (i < keyList.size() - 1){
                //如果不是最后一对键值,则每对键值后需加逗号
                res += ",\n";
            }else {
                res += "\n";
            }
        }
        res += "}";
        return res;
    }

    private String andQuetes(String s) {
        //添加双引号
        return "\"" + s +"\"";
    }
自定义GetKeyValueList类
public class GetKeyValueList {
        private String transtxt;
        private String spA;
        private String spB;
        private List<String> keyList;
        private List<String> valueList;

        public GetKeyValueList(String transtxt, String spA, String spB) {
            this.transtxt = transtxt;
            this.spA = spA;
            this.spB = spB;
        }

    public List<String> getKeyList() {
        return keyList;
    }

    public List<String> getValueList() {
        return valueList;
    }

    public GetKeyValueList invoke() {
        //以下集合用户存取键值
        keyList = new ArrayList<>();
        valueList = new ArrayList<>();

        //对tanstxt进行分割,先用“;”将各对象分开
        String[] objs = transtxt.split(spB);

        //遍历对象,逐条将键与值分割
        for (String obj: objs) {
            String[] split = obj.split(spA);
            //分割后,前面部分为键,后面为值,并将其添加到集合中
            keyList.add(split[0]);
            valueList.add(split[1]);
        }
        return this;
    }
}
posted @ 2024-05-30 17:30  EQ-雪梨蛋花汤  阅读(14)  评论(0)    收藏  举报