0

How to convert this code into java code

$response = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $response);

I try this

String jsonStr = "Some Json string";
jsonStr.replaceAll("/[\x00-\x1F\x80-\xFF]/", "");

But it show an error

Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )
Nur Uddin
  • 2,778
  • 6
  • 16
  • 22

1 Answers1

1

You have to escape the backslashes with another backslash like this:

jsonStr.replaceAll("[\\x00-\\x1F\\x80-\\xFF]", "");

Backslashes in strings introduce special characters in Java.

stevecross
  • 5,588
  • 7
  • 47
  • 85
  • I am getting a json string from a website. json string contains ` ` in start point. Now I how I remove this. I try with this code in `php` and it works for me. but I cant do it in android. can you please give any advice? – Nur Uddin Aug 24 '15 at 09:34
  • In Java you don't need a delimiter for the regex. Thus removing the slashes should give you the same result as in PHP. I updated the answer. – stevecross Aug 24 '15 at 10:16