JAVA删除字符串固定下标的字串

Posted Recently 祝祝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        
    

搞定。

end》》》
问题 = 机会。遇到问题的时候,内心其实是开心的,越大的问题意味着越大的机会。

以上是关于JAVA删除字符串固定下标的字串的主要内容,如果未能解决你的问题,请参考以下文章

Python学习笔记-小记

Java 判断以数字开头的字串的正则表示式怎么写?

UnityC#字串如何获取换行符号-最基础的字串处理方法,也最容易常见错误

UnityC#字串如何获取换行符号-最基础的字串处理方法,也最容易常见错误

wordwrap — 打断字符串为指定数量的字串

wordwrap — 打断字符串为指定数量的字串