zahir hussain zahir hussain - 5 months ago 7
Java Question

How to remove the backslash in string using regex in Java?

How to remove the backslash in string using regex in Java?

For example:

hai how are\ you?


I want only:

hai how are you?

Answer
str = str.replaceAll("\\\\", "");

or

str = str.replace("\\", "");

replaceAll() treats the first argument as a regex, so you have to double escape the backslash. replace() treats it as a literal string, so you only have to escape it once.

Comments