简体   繁体   中英

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("\\\\\\\\",""); ? (see 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).

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 "\\\\." 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],
[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]]]}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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