Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java String replaceAll-以一种独特的方式为\_Java_String_Replaceall - Fatal编程技术网

Java String replaceAll-以一种独特的方式为\

Java String replaceAll-以一种独特的方式为\,java,string,replaceall,Java,String,Replaceall,我有这样一个字符串: My word is "I am busy" message String test = "My word is \\\"I am busy\\\" message"; 现在,当我将此字符串分配给pojo字段时,我得到的转义如下: String test = "My word is \"I am busy\" message"; 我还有一些其他数据,我想用上面的字符串替换其中的某些内容: 假设我的基本字符串是: String s = "There is some __

我有这样一个字符串:

My word is "I am busy" message
String test = "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
There is some My word is "I am busy" message to be replaced here
为什么在我替换之后“\”没有出现在中。我需要逃两次吗

也可以这样使用:

My word is "I am busy" message
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
我的预期输出是:

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

您需要使用四个反斜杠来打印一个反斜杠

String test = "My word is \\\\\"I am busy\\\\\" message";
String s = "There is some __data to be replaced here";
System.out.println(s.replaceAll("__data", test));

String test = "My word is \"I am busy\" message";
String s = "There is some __data to be replaced here";
System.out.println(s.replaceAll("__data", test.replace("\"", "\\\\\"")));
输出:

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

您需要使用四个反斜杠来打印一个反斜杠

String test = "My word is \\\\\"I am busy\\\\\" message";
String s = "There is some __data to be replaced here";
System.out.println(s.replaceAll("__data", test));

String test = "My word is \"I am busy\" message";
String s = "There is some __data to be replaced here";
System.out.println(s.replaceAll("__data", test.replace("\"", "\\\\\"")));
输出:

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
试试这个:

String test = "My word is \\\\\"I am busy\\\\\" message";
String s = "There is some __data to be replaced here";
System.out.println(s.replaceAll("__data", test));
要在输出中获取
\
,需要使用
\

从:

请注意,替换中的反斜杠()和美元符号($) 字符串可能会导致结果与正在使用的结果不同 视为文字替换字符串;见Matcher.replaceAll。使用 Matcher.quoteReplacement(java.lang.String)来抑制特殊的 如果需要,这些字符的含义

所以你可以用

试试这个:

String test = "My word is \\\\\"I am busy\\\\\" message";
String s = "There is some __data to be replaced here";
System.out.println(s.replaceAll("__data", test));
要在输出中获取
\
,需要使用
\

从:

请注意,替换中的反斜杠()和美元符号($) 字符串可能会导致结果与正在使用的结果不同 视为文字替换字符串;见Matcher.replaceAll。使用 Matcher.quoteReplacement(java.lang.String)来抑制特殊的 如果需要,这些字符的含义

所以你可以用


您的预期输出是什么?可能重复@PM77-1我不想替换原始
字符串中的“\”这个“\”,因此编译器会忽略它们,如果您打印原始
字符串,您将得到
我的单词是“我很忙”消息
为什么不使用s.replace而不是s.replaceAll您预期的输出是什么?可能是@PM77-1的重复项我不想替换原始
字符串
中的“\”所以编译器会忽略它们,如果您打印原始
字符串
您将得到
我的话是“我很忙”消息
为什么不使用s.replace而不是s.replaceAll我无法控制添加反斜杠,我正在从外部获取数据。你的意思是我逃避了两次吗?我无法控制添加反斜杠,我从外部获取数据。你的意思是我逃避了两次吗?我无法控制添加反斜杠,我从外部获取数据。你的意思是我逃避了两次吗?我无法控制添加反斜杠,我从外部获取数据。你是说我逃过两次?