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  \"  \'  \\ )
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.

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.