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

其他教程   发布日期:2025年01月16日   浏览次数:184

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

需要修改的报文

当你拿到的报文是这样的

  1. {
  2. "input": {
  3. "sdfsn": "23u4209350-2",
  4. "fsfs": "128412094",
  5. "sgsgsg": "15821059",
  6. "inssgsuplc_admdfdfdvs": "125125332",
  7. "dgh": "125215312",
  8. "dfgdfg": "215215",
  9. "sdhdsh": "",
  10. "sdfsn": "",
  11. "shdfshdshdsh": "shsdh",
  12. "sdhdsh": "shsh.0",
  13. "shsdhsd": "1",
  14. "shsdh": "1607",
  15. "input": {
  16. "data": {
  17. "dhfsdhsd": "235325",
  18. "shsdhsdh": "03",
  19. "dgd": "BE0445360",
  20. "dfhfdh": "11",
  21. "dshshsd": 76.56,
  22. "ghjrfgj": "01",
  23. "grjf": "234623626",
  24. "hjfd": "236436",
  25. "djfdfgjdfj": "45634",
  26. "exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",
  27. "dfjgdfj": "",
  28. "dfjdfjgdfj": "56745745",
  29. "dfgjdfgjh": 45756758,
  30. "jdfgjhfdgj": 0,
  31. }
  32. }
  33. },
  34. "output": {
  35. "output": {
  36. "r757": {
  37. "dhfsdhsd": "235325",
  38. "shsdhsdh": "03",
  39. "dgd": "BE0445360",
  40. "dfhfdh": "11",
  41. "dshshsd": 76.56,
  42. "ghjrfgj": "01",
  43. "grjf": "234623626",
  44. "hjfd": "236436",
  45. "djfdfgjdfj": "45634",
  46. "exp_content": "",
  47. "dfjgdfj": "",
  48. "dfjdfjgdfj": "56745745",
  49. "dfgjdfgjh": 45756758,
  50. "jdfgjhfdgj": 0,
  51. },
  52. "sdfgsdfg": [
  53. {
  54. "sgasgag": "4673476",
  55. "agasgdas": 5675467,
  56. "asgasgasg": "",
  57. "asdgasgas": 4567456754,
  58. "dhsdsxchsdh": 54675467,
  59. "sdfhsdhsdh": "5674756457"
  60. }
  61. ]
  62. },
  63. "erherth": 0,
  64. }
  65. }

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

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

实现代码如下

  1. private JSONObject resolveApplicationJson(HttpServletRequest request) {
  2. InputStream is = null;
  3. String json = null;
  4. try {
  5. is = request.getInputStream();
  6. json = IOUtils.toString(is, "UTF-8");
  7. json=json.replaceAll("
  8. |
  9. | ","");
  10. int index= json.indexOf("exp_Content");
  11. int indexfirst=json.indexOf("{", index);
  12. int indexlast=json.indexOf("}",index);
  13. if (index!=-1 && indexlast !=-1 &&indexfirst !=-1) {
  14. StringBuffer stringBuffer = new StringBuffer(json);
  15. stringBuffer.delete(indexfirst,indexlast+1);
  16. json=stringBuffer.toString();
  17. }
  18. } catch (IOException e) {
  19. throw new RuntimeException("CANNOT get reader from request!", e);
  20. } finally {
  21. if (is != null) {
  22. try {
  23. is.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. try {
  30. return new JSONObject(json);
  31. } catch (JSONException e) {
  32. throw new RuntimeException("CANOT CONVET JSON:[" + json + "] to JSONObject!", e);
  33. }
  34. }

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

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

  1. {
  2. "input": {
  3. "sdfsn": "23u4209350-2",
  4. "fsfs": "128412094",
  5. "sgsgsg": "15821059",
  6. "inssgsuplc_admdfdfdvs": "125125332",
  7. "dgh": "125215312",
  8. "dfgdfg": "215215",
  9. "sdhdsh": "",
  10. "sdfsn": "",
  11. "shdfshdshdsh": "shsdh",
  12. "sdhdsh": "shsh.0",
  13. "shsdhsd": "1",
  14. "shsdh": "1607",
  15. "input": {
  16. "data": {
  17. "dhfsdhsd": "235325",
  18. "shsdhsdh": "03",
  19. "dgd": "BE0445360",
  20. "dfhfdh": "11",
  21. "dshshsd": 76.56,
  22. "ghjrfgj": "01",
  23. "grjf": "234623626",
  24. "hjfd": "236436",
  25. "djfdfgjdfj": "45634",
  26. "exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",
  27. "dfjgdfj": "",
  28. "dfjdfjgdfj": "56745745",
  29. "dfgjdfgjh": 45756758,
  30. "jdfgjhfdgj": 0,
  31. }
  32. }
  33. },
  34. "output": {
  35. "output": {
  36. "r757": {
  37. "dhfsdhsd": "235325",
  38. "shsdhsdh": "03",
  39. "dgd": "BE0445360",
  40. "exp_Content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",
  41. "dfhfdh": "11",
  42. "dshshsd": 76.56,
  43. "ghjrfgj": "01",
  44. "grjf": "234623626",
  45. "hjfd": "236436",
  46. "djfdfgjdfj": "45634",
  47. "exp_content": "{"gdsg":"01","gjfj":"658568","fjfj":"5675467","ghfjfgkj":"68568","vmgfvj":"658568","gfhjgfyk":"0","fghkfghkg":"5474567"}",
  48. "dfjgdfj": "",
  49. "dfjdfjgdfj": "56745745",
  50. "dfgjdfgjh": 45756758,
  51. "jdfgjhfdgj": 0,
  52. },
  53. "sdfgsdfg": [
  54. {
  55. "sgasgag": "4673476",
  56. "agasgdas": 5675467,
  57. "asgasgasg": "",
  58. "asdgasgas": 4567456754,
  59. "dhsdsxchsdh": 54675467,
  60. "sdfhsdhsdh": "5674756457"
  61. }
  62. ]
  63. },
  64. "erherth": 0,
  65. }
  66. }

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

实现代码如下

  1. # 全局变量
  2. private static String REGEX = "exp_[c,C]ontent";
  3. # 方式方法
  4. Pattern p = Pattern.compile(REGEX);
  5. // 获取 matcher 对象
  6. Matcher m = p.matcher(JSON);
  7. List<Integer> list = new ArrayList();
  8. while(m.find()){
  9. list.add(m.start());
  10. }
  11. int index=0;
  12. int indexfirst=0;
  13. int indexlast=0;
  14. for (int j = 0; j < list.size(); j++) {
  15. index=JSON.indexOf("exp_",indexlast);
  16. indexfirst=JSON.indexOf("{", index);
  17. indexlast=JSON.indexOf("}",index);
  18. StringBuffer stringBuffer =null;
  19. if (indexlast !=-1 &&indexfirst !=-1) {
  20. stringBuffer = new StringBuffer(JSON);
  21. stringBuffer.delete(indexfirst,indexlast+1);
  22. }
  23. JSON=stringBuffer.toString();
  24. }

以上就是JAVA怎么删除字符串固定下标字串的详细内容,更多关于JAVA怎么删除字符串固定下标字串的资料请关注九品源码其它相关文章!