简体   繁体   English

用字符串java替换字符串

[英]replace string with a string java

I have a weird problem . 我有一个奇怪的问题。

I am calling a web service in my application and the response sometimes printed like this in the console "\\n" now i want to is to replace it with an empty brackets so it will be printed like this "" 我正在应用程序中调用Web服务,有时响应有时会在控制台中以"\\n"这样打印,现在我要用一个空括号替换它,以便将其打印为""

so what i have tried 所以我尝试过

String serverResponse = Util.callService(.....);
System.out.println(serverResponse.replace("\"\\n\"","\"\"");

but the output still prints "\\n" not "" 但输出仍显示"\\n"而不是""

when i hard code the string , it works 当我对字符串进行硬编码时,它可以工作

for example 例如

String str = ""\"\\n\""; // equals to "\n" in console 
 System.out.println(serverResponse.replace("\"\\n\"","\"\""); //prints ""

what is the problem ? 问题是什么 ? is it encoding thing ? 是编码的东西吗? why the first code does not work with server response ?? 为什么第一个代码不能与服务器响应一起使用?

Note: I have no control over the web service. 注意:我无法控制Web服务。

I solve it it requires 4 back slash, not two: 我解决了它需要4个反斜杠,而不是两个:

String serverResponse = Util.callService(.....);
System.out.println(serverResponse.replace("\"\\\\n\"","\"\"");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM