SoFunction
Updated on 2025-03-08

Implementation of JAVA deletion string fixed subscript string

This solution is applicable in enterprises and is suitable for primary Java development and reference.

Messages that need to be modified

When the message you get is like this

{
    "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,
    }
}

From this look, you will know that the above message will definitely report an error in the postman because exp_Content, so it is not used, so you want to delete it. Actually it's not that difficult to delete

That is, the streaming string is used. Search for fixed characters in strings, then perform string-to-character streams, delete fixed characters in the character stream, and then turn them back. Because the string is already final, many usages are implemented using string to character stream

The implementation code is as follows

    private JSONObject resolveApplicationJson(HttpServletRequest request) {
        InputStream is = null;
        String json = null;
        try {
            is = ();
            json = (is, "UTF-8");
            json=("\\r|\n|\t","");
            int index=  ("exp_Content");
            int indexfirst=("{", index);
            int indexlast=("}",index);
            if (index!=-1 && indexlast !=-1 &&indexfirst !=-1) {
            	  StringBuffer stringBuffer = new StringBuffer(json);
                  (indexfirst,indexlast+1);
                  json=();
			}
          
     
        } catch (IOException e) {
            throw new RuntimeException("CANNOT get reader from request!", e);	
        } finally {
            if (is != null) {
                try {
                    ();
                } catch (IOException e) {
                    ();
                }
            }
        }

        try {
            return new JSONObject(json);
        } catch (JSONException e) {
            throw new RuntimeException("CANOT CONVET JSON:[" + json + "] to JSONObject!", e);
        }
    }

How do you delete more than one data that does not meet the regulations?

This is also very simple. If you read this article, you need a better solution. We look forward to discussing it together and learning progress (^-^)V

When you get the message, it's like this.

{
    "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,
    }
}

The solution is as follows: write out the regular expression as needed, then use the regular to see how many such unwanted symbols are there, then loop through it several times, then find this part, and finally delete it.

The implementation code is as follows

# Global variablesprivate static String REGEX = "exp_[c,C]ontent";

# MethodPattern p = (REGEX);
        // Get the matcher object        Matcher m = (JSON);
        List<Integer> list = new ArrayList();
        while(()){
            (());
        }
        int index=0;
        int indexfirst=0;
        int indexlast=0;
        for (int j = 0; j < (); j++) {
             index=("exp_",indexlast);
             indexfirst=("{", index);
             indexlast=("}",index);
            StringBuffer stringBuffer =null;
            if (indexlast !=-1 &&indexfirst !=-1) {
                stringBuffer = new StringBuffer(JSON);
                (indexfirst,indexlast+1);
            }
            JSON=();
        }

This is the article about the implementation of JAVA deletion of strings fixed subscript strings. For more information about JAVA deletion of subscript strings, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!