0

I have intelliJ running with Java/REST API and having trouble removing and replacing string in a certain format.

Response string :

{"isSuccess":[{"frequency":}]}]}}{"isSuccess":[{"frequency":}]}]}}{"isSuccess":<testData>[{"frequency":<testData>}]}]}}

Expected string :

[{"frequency":}]},{"frequency":<testData>}]},{"frequency":<testData>}}]}]}}

Actual String :

[{"frequency":<testData>}]}]}}[{"frequency":<testData>}]}]}}[{"frequency":"<testData>}]}]}}

Code :

public static String getResponseString(String response){
        String getIDErrorCodeString = response.split("\\[")[0];                 
        String getStringWithoutIDErrorCode = response.replaceAll(Pattern.quote(getIDErrorCodeString), "");
        String getStringwithBracket = "}]}]" + getStringWithoutIDErrorCode + "}}[{";

        String actualResults = getStringWithoutIDErrorCode.replaceAll(Pattern.quote(getStringwithBracket), ",");

        return actualResults;
}

The original string had the }}[{ in the beginning of the line which got removed in the actual results but still pattern is not recognizing and finding the correct string to replace not just in the beginning but in the entire string. Anyone knows how to do this?

4
  • Please update the question with the original input string. Commented Nov 9, 2023 at 16:25
  • @Bubbles so you need to feed the Response string and remove the }}[{ characters from it right ? Commented Nov 9, 2023 at 16:43
  • Yes but not all }]}]}}[{ needs to be replaced. At the end we need to keep it. So how do I replace it only in the middle ? Commented Nov 9, 2023 at 16:46
  • It should look identical to expected results basically Commented Nov 9, 2023 at 16:47

2 Answers 2

0

Start by removing all occurrences of {"isSuccess":<testData>.

s = s.replace("{\"isSuccess\":<testData>", "");

Then, replace ]}}[, with ,.

s = s.replace("]}}[", ",");

And finally, insert a }, at n − 6.

s = s.substring(0, s.length() - 6) + '}' + s.substring(s.length() - 6);

Output

[{"frequency":<testData>}]},{"frequency":<testData>}]},{"frequency":<testData>}}]}]}}
Sign up to request clarification or add additional context in comments.

Comments

0

It was very simple than I wrote originally,

public static String getResponseString(String response){
        String getIDErrorCodeString = response.split("\\[")[0];                  
        
        String getStringWithoutIDErrorCode = response.replaceAll(Pattern.quote(getIDErrorCodeString), "");
        String actualResults = getStringWithoutIDErrorCode.replaceAll("]}}\\[", ",");

    return actualResults;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.