文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

JAVA怎么删除字符串固定下标字串

2023-07-06 01:08

关注

本篇内容主要讲解“JAVA怎么删除字符串固定下标字串”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“JAVA怎么删除字符串固定下标字串”吧!

需要修改的报文

当你拿到的报文是这样的

{    "input": {        "sdfsn": "23u4209350-2",        "fsfs": "128412094",        "sgsgsg": "15821059",        "inssgsuplc_admdfdfdvs": "125125332",        "dgh": "125215312",        "dfgdfg": "215215",        "sdhdsh": "",        "sdfsn": "",        "shdfshdshdsh": "shsdh",        "sdhdsh": "shsh.0",        "shsdhsd": "1",        "shsdh": "1607",        "input": {            "data": {                "dhfsdhsd": "235325",                "shsdhsdh": "03",                "dgd": "BE0445360",                "dfhfdh": "11",                "dshshsd": 76.56,                "ghjrfgj": "01",                "grjf": "234623626",                "hjfd": "236436",                "djfdfgjdfj": "45634",                "exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",                "dfjgdfj": "",                "dfjdfjgdfj": "56745745",                "dfgjdfgjh": 45756758,                "jdfgjhfdgj": 0,            }        }    },    "output": {        "output": {            "r757": {                "dhfsdhsd": "235325",                "shsdhsdh": "03",                "dgd": "BE0445360",                "dfhfdh": "11",                "dshshsd": 76.56,                "ghjrfgj": "01",                "grjf": "234623626",                "hjfd": "236436",                "djfdfgjdfj": "45634",                "exp_content": "",                "dfjgdfj": "",                "dfjdfjgdfj": "56745745",                "dfgjdfgjh": 45756758,                "jdfgjhfdgj": 0,            },            "sdfgsdfg": [                {                    "sgasgag": "4673476",                    "agasgdas": 5675467,                    "asgasgasg": "",                    "asdgasgas": 4567456754,                    "dhsdsxchsdh": 54675467,                    "sdfhsdhsdh": "5674756457"                }            ]        },        "erherth": 0,    }}

这一看就知道上边的报文在postman里边肯定会报错,因为exp_Content,因此他又没有用到,所以你想把他删掉。其实也没那么难删

也就是用到了流转字符串。字符串固定字符查找,然后进行字符串转字符流,删掉字符流中固定字符,之后再转回来。因为字符串已经是final了所以很多用法都是使用字符串转字符流实现的

实现代码如下

    private JSONObject resolveApplicationJson(HttpServletRequest request) {        InputStream is = null;        String json = null;        try {            is = request.getInputStream();            json = IOUtils.toString(is, "UTF-8");            json=json.replaceAll("\\r|\n|\t","");            int index=  json.indexOf("exp_Content");            int indexfirst=json.indexOf("{", index);            int indexlast=json.indexOf("}",index);            if (index!=-1 && indexlast !=-1 &&indexfirst !=-1) {              StringBuffer stringBuffer = new StringBuffer(json);                  stringBuffer.delete(indexfirst,indexlast+1);                  json=stringBuffer.toString();}                       } catch (IOException e) {            throw new RuntimeException("CANNOT get reader from request!", e);        } finally {            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        try {            return new JSONObject(json);        } catch (JSONException e) {            throw new RuntimeException("CANOT CONVET JSON:[" + json + "] to JSONObject!", e);        }    }

多存在多个不符合规定的数据然后你要删掉怎么操作呢?

当你拿到的报文是这样的。

{    "input": {        "sdfsn": "23u4209350-2",        "fsfs": "128412094",        "sgsgsg": "15821059",        "inssgsuplc_admdfdfdvs": "125125332",        "dgh": "125215312",        "dfgdfg": "215215",        "sdhdsh": "",        "sdfsn": "",        "shdfshdshdsh": "shsdh",        "sdhdsh": "shsh.0",        "shsdhsd": "1",        "shsdh": "1607",        "input": {            "data": {                "dhfsdhsd": "235325",                "shsdhsdh": "03",                "dgd": "BE0445360",                "dfhfdh": "11",                "dshshsd": 76.56,                "ghjrfgj": "01",                "grjf": "234623626",                "hjfd": "236436",                "djfdfgjdfj": "45634",                "exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",                "dfjgdfj": "",                "dfjdfjgdfj": "56745745",                "dfgjdfgjh": 45756758,                "jdfgjhfdgj": 0,            }        }    },    "output": {        "output": {            "r757": {                "dhfsdhsd": "235325",                "shsdhsdh": "03",                "dgd": "BE0445360",                "exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",                "dfhfdh": "11",                "dshshsd": 76.56,                "ghjrfgj": "01",                "grjf": "234623626",                "hjfd": "236436",                "djfdfgjdfj": "45634",                "exp_content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",                "dfjgdfj": "",                "dfjdfjgdfj": "56745745",                "dfgjdfgjh": 45756758,                "jdfgjhfdgj": 0,            },            "sdfgsdfg": [                {                    "sgasgag": "4673476",                    "agasgdas": 5675467,                    "asgasgasg": "",                    "asdgasgas": 4567456754,                    "dhsdsxchsdh": 54675467,                    "sdfhsdhsdh": "5674756457"                }            ]        },        "erherth": 0,    }}

解决方式如下:根据需要写出正则表达式,再使用正则看看有几个这样的不需要的符号,然后循环遍历几次,之后找到这部分,最后删除掉。

实现代码如下

# 全局变量private static String REGEX = "exp_[c,C]ontent";# 方式方法Pattern p = Pattern.compile(REGEX);        // 获取 matcher 对象        Matcher m = p.matcher(JSON);        List<Integer> list = new ArrayList();        while(m.find()){            list.add(m.start());        }        int index=0;        int indexfirst=0;        int indexlast=0;        for (int j = 0; j < list.size(); j++) {             index=JSON.indexOf("exp_",indexlast);             indexfirst=JSON.indexOf("{", index);             indexlast=JSON.indexOf("}",index);            StringBuffer stringBuffer =null;            if (indexlast !=-1 &&indexfirst !=-1) {                stringBuffer = new StringBuffer(JSON);                stringBuffer.delete(indexfirst,indexlast+1);            }            JSON=stringBuffer.toString();        }

到此,相信大家对“JAVA怎么删除字符串固定下标字串”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯