java 字符串插入字符

需求: 

编号加括号,eg:
   "编号": "鲁H安许证字2020070001号" ==> (鲁)H安许证字[2020]070001号
package com.riskeys.cbs.third.api.utils;

import lombok.extern.slf4j.Slf4j;

/**
 * @Title: Demo
 * @Author: ken
 * @Description:
 * @Date: 2022/7/6  15:57
 **/
@Slf4j
public class Demo {
    public static void main(String[] args) {
        String a =  "\"a  b a     a,编号\": \"鲁H安许证字2020070001号\"";
        String b = a.replaceAll("\\s*", "");
        //构造一个StringBuilder对象
        StringBuilder sb = new StringBuilder(b);
        int idx = b.indexOf("编号");
        log.info("编号 index is :{}", idx);
        sb.insert(idx+5,"(");
        sb.insert(idx+7,")");
        log.info("加小括号:{}", sb.toString());

        int idxs = b.indexOf("许证字");
        log.info("许证字 index is :{}", idxs);
        sb.insert(idxs+5,"[");
        sb.insert(idxs+10,"]");
        log.info("加中括号 :{}", sb.toString());
    }
}

你可能感兴趣的:(java,java)