中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JAVA怎么刪除字符串固定下標字串

發布時間:2023-04-11 11:30:43 來源:億速云 閱讀:104 作者:iii 欄目:開發技術

本篇內容主要講解“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怎么刪除字符串固定下標字串”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

镇巴县| 西林县| 庆城县| 西平县| 阜康市| 靖江市| 大埔县| 武宣县| 措美县| 松潘县| 神农架林区| 古田县| 斗六市| 易门县| 安徽省| 金阳县| 太仆寺旗| 凤冈县| 临海市| 宁晋县| 织金县| 偏关县| 青田县| 竹溪县| 米林县| 河北区| 砀山县| 新巴尔虎右旗| 尼木县| 昌江| 房山区| 灵丘县| 五常市| 武乡县| 玉溪市| 贵州省| 南安市| 彰武县| 镇巴县| 嵊泗县| 清徐县|