开发者

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

开发者 https://www.devze.com 2022-12-19 17:03 出处:网络
How to remove the backslash in string using regex in Java? For example: hai how are\\ you? I want only: hai how are 开发者_运维知识库you?

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

For example:

hai how are\ you?

I want only:

hai how are 开发者_运维知识库you?


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.


You can simply use String.replaceAll()

 String foo = "hai how are\\ you?";
 String bar = foo.replaceAll("\\\\", "");


String foo = "hai how are\ you?"; String bar = foo.replaceAll("\\", ""); Doesnt work java.util.regex.PatternSyntaxException occurs.... Find out the reason!! @Alan has already answered.. good

String bar = foo.replace("\\", ""); Does work

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号