【发布时间】:2015-04-15 05:15:17
【问题描述】:
我有一个这样的字符串:
My word is "I am busy" message
现在,当我将此字符串分配给 pojo 字段时,我得到如下转义:
String test = "My word is \"I am busy\" message";
我有一些其他数据,我希望用上面的字符串替换一些内容:
假设我的基本字符串是:
String s = "There is some __data to be replaced here";
现在我使用 replaceAll 时:
String s1 = s.replaceAll("__data", test);
System.out.println(s1);
这会将输出返回为:
There is some My word is "I am busy" message to be replaced here
为什么我替换后没有出现“\”。我需要逃避它 2 次吗?
当这样使用它时:
String test = "My word is \\\"I am busy\\\" message";
然后它也给出与 :
相同的输出There is some My word is "I am busy" message to be replaced here
我的预期输出是:
There is some My word is \"I am busy\" message to be replaced here
【问题讨论】:
-
你的预期输出是什么?
-
@PM77-1 我不想替换“\”
-
原始
String中的“\”在那里,因此编译器会忽略它们,如果您打印原始String,您将得到My word is "I am busy" message -
为什么不使用 s.replace 而不是 s.replaceAll
标签: java string replaceall