简体   繁体   English

如何在Java中用引号替换子字符串?

[英]How to replace substring with quotes in Java?

I have a string: 我有一个字符串:

str="{\"type\":\"Polygon\",\"coordinates\":[[[60.677938980978993,56.834449959232998],
[60.680103564162927,56.834305549577387],[60.679971577383398,56.833005837614643],
[60.679813193248009,56.832846980836457],[60.678546120164683,56.832760331400671],
[60.678229351893869,56.831850500219574],[60.677622212708137,56.831893826015182],
[60.676830292031028,56.832139337910085],[60.67624955020122,56.832818097713471],
[60.676275947556981,56.833655699235088],[60.676328742268893,56.834724336044253],
[60.677411033860842,56.834695454369324],[60.677490225928629,56.834478841097273],
[60.677938980978993,56.834449959232998]]]}"

Now i want to delete all \\ : 现在我要删除所有\\

str.replaceAll("\\",""); 

And get error: 并得到错误:

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\

Okey i says. 哦,我说。 Another wish its delete first and last quotes: 另一个希望删除其第一引号和最后引号:

str.substring(str.indexOf("\""),str.lastIndexOf("\""));

And i get a string: 我得到一个字符串:

"{\"type\":\"Polygon\",\"coordinates\":[[[60.677938980978993,56.834449959232998],
[60.680103564162927,56.834305549577387],[60.679971577383398,56.833005837614643],
[60.679813193248009,56.832846980836457],[60.678546120164683,56.832760331400671],
[60.678229351893869,56.831850500219574],[60.677622212708137,56.831893826015182],
[60.676830292031028,56.832139337910085],[60.67624955020122,56.832818097713471],
[60.676275947556981,56.833655699235088],[60.676328742268893,56.834724336044253],
[60.677411033860842,56.834695454369324],[60.677490225928629,56.834478841097273],
[60.677938980978993,56.834449959232998]]]"

Only last } was deleted. 仅最后一个}被删除。

What i doing wrong in this code snippets? 我在这段代码片段中做错了什么?

Have you tried str.replaceAll("\\\\\\\\",""); 您是否尝试过str.replaceAll("\\\\\\\\",""); ? (see https://stackoverflow.com/a/3640386/500478 ) (请参阅https://stackoverflow.com/a/3640386/500478

Use either of one, 使用其中之一,

str.replaceAll("\\.", "");

or 要么

 str.replaceAll("\\\\", "");

When you type "\\\\" , this is actually a single backslash (due to escaping special characters in Java Strings). 当您键入"\\\\" ,实际上是一个反斜杠(由于转义了Java字符串中的特殊字符)。

Regular expressions also use backslash as special character, and you need to escape it with another backslash or using a DOT(.) So in the end, you need to pass "\\\\\\\\" or "\\\\." 正则表达式还使用反斜杠作为特殊字符,因此您需要使用另一个反斜杠或使用DOT(.)对其进行转义DOT(.)因此,最后,您需要传递"\\\\\\\\""\\\\." as pattern to match a single backslash. 作为匹配单个反斜杠的模式。

       System.out.println(str.replaceAll("\\\\", ""));

Output: 输出:

{"type":"Polygon","coordinates":[[[60.677938980978993,56.834449959232998], [60.680103564162927,56.834305549577387],[60.679971577383398,56.833005837614643],[60.679813193248009,56.832846980836457],[60.678546120164683,56.832760331400671], {“ type”:“ Polygon”,“ coordinates”:[[[[60.677938980978993,56.834449959232998],[60.680103564162927,56.834305549577387],[60.679971577383398,56.833005837614643],[60.679813193248009,56.832846980836457],[60.678546120
[60.678229351893869,56.831850500219574],[60.677622212708137,56.831893826015182], [60.678229351893869,56.831850500219574],[60.677622212708137,56.831893826015182],
[60.676830292031028,56.832139337910085],[60.67624955020122,56.832818097713471], [60.676830292031028,56.832139337910085],[60.67624955020122,56.832818097713471],
[60.676275947556981,56.833655699235088],[60.676328742268893,56.834724336044253], [60.677411033860842,56.834695454369324],[60.677490225928629,56.834478841097273],[60.677938980978993,56.834449959232998]]]} [60.676275947556981,56.833655699235088],[60.676328742268893,56.834724336044253],[60.677411033860842,56.834695454369324],[60.677490225928629,56.834478841097273],[60.677938980978993,56.834449959232998]]]]

str.replace("\\", "");

Works fine for your case. 适用于您的情况。

public String replace(CharSequence target,CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. 用指定的文字替换序列替换此字符串中与文字目标序列匹配的每个子字符串。

String s = "abc" + "\\" + "def";
System.out.println(s);

And it prints out the following - 它打印出以下内容-

abc\def

The \\ is an escape character in both the String and in regex. \\是字符串和正则表达式中的转义字符。

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

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