Home > Software engineering >  How do I remove all control characters in a string in Java?
How do I remove all control characters in a string in Java?

Time:02-14

My REST API, which is build with Spring in Java, produces an invalid JSON object, because it contains multiple breaks in a string, which lead to the problem, that the string has an unexpected end and the rest doesn't count as part of the string anymore, example:

{
   "status": "Success",
   "message": "Lorem ipsum",
   "data": {
      "correct": [
         {
            "record": "ULTRA LONG
                 XML STRING
                       WITH BREAKS",
            "code": 0,
            "errors": []
         }
      ]
   }
}

The error arises in the data -> correct -> record string field, because it contains breaks which splits the original string.

My API endpoint serializes the above JSON like this:

@PostMapping(value="/check-records", 
    consumes=MediaType.APPLICATION_JSON_VALUE,
    produces=MediaType.APPLICATION_JSON_VALUE)
public Response checkRecords(@RequestBody(required=true) Records records) {
   // Check records
   return new Response("Success", "Lorem ipsum", data);
}

Response is a class, which automatically gets serialized into a JSON object after returning. data is a map in order to create the above JSON structure.

I couldn't find any suitable solution for my problem yet. Does anybody has an idea how I could remove all breaks, spaces or control characters before I serialize the JSON object?

I appreciate any kind of help, sheers! :)

CodePudding user response:

Thanks to @pringi. He suggested to use a regex to remove all control characters in Java before I serialize the JSON object.

String record = orginalRecord.replaceAll("[\\\\p{Cntrl}^\\r\\n\\t] ", "")

You can find more informations about regex in the original question: How to remove control characters from java string?

  • Related