Alex Rivera | Logout

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

Asked 2010-02-11T05:09:27.490
48

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

For example:

hai how are\ you?

I want only:

hai how are you?
Edit
Report

1 Answer

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

answered 2010-02-11T05:33:14.183

Your Answer