Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the substitute for the backslash of escape characters in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the replacement of escaped character backslash in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. demand

There is a string str

String str = "{\\" name\\ ":\\" spy\\ ",\\" id\ ":\\" 123456\\ "}; System.out.println (" str = "+ str)

The output on the console is:

Str = {\ "name\":\ "spy\",\ "id\":\ "123456\"}

Goal: convert str to the standard json format string str1 to call the method of JsonUtil to convert the string to map. That is, the target str1 is:

Str1 = {"name": "spy", "id": "123456"}

Second, the realization method

1. Using escaping and anti-escaping in StringEscapeUtils

One of the useful utility classes for dealing with strings in the apache toolkit common-lang is StringEscapeUtils. It can be used to escape and reverse the meaning of html, xml, java and so on.

String str = "{\\" name\\ ":\\" spy\\ ",\\" id\ ":\\" 123456\\ "}"; System.out.println ("original str =" + str "); String str1 = StringEscapeUtils.unescapeJava (str); System.out.println (" target str1 = "+ str1); / / StringEscapeUtils.escapeJava (str1) of the corresponding method; / / str1 can be escaped back to str

Console output:

Original str = {\ "name\":\ "spy\",\ "id\":\ "123456\"} Target str1 = {"name": "spy", "id": "123456"}

two。 Using the replaceAll method of Java

String str1 = str.replaceAll ("\", ""); / / the output of console str1 is: {"name": "spy", "id": "123456"}

Third, the analysis of the connotation of replaceAll of Java.

Use the replaceAll (String regex, String replacement) function of Java, which replaces all regex matches with replacement. Regex is a regular expression and replacement is a string.

String str = "{\\" name\\ ":\\" spy\\ ",\\" id\ ":\\" 123456\\ "}"

(1) escape the string str,Java,\\ means\,\ "signify", so it is {\ "name\":\ "spy\",\ "id\":\ "123456\"} in Java memory. However, our goal is {"name": "spy", "id": "123456"}, replacing the escape character\ with blank.

(2) the replaceAll (String regex, String replacement) function of Java, the first argument is a regular expression. The "\" in the regular expression and the character that follows form an escape character, which represents a special meaning, such as "\ n" for line breaks, and so on. Therefore, if you want to represent a backslash\ in a regular expression, you should use\\. However, the parameter regex is first escaped by Java. To express two backslashes\\, you need four backslashes.

To sum up: the first parameter of replaceAll is a regular expression, so it has to be escaped twice, once Java, once regular. So you need four backslashes to match one backslash. Therefore, the code to replace a replaceAll with an empty backslash is:

Str1 = str.replaceAll ("\", "")

IV. Supplementary explanation

The replaceAll of String is the same as Matcher.replaceAll. The following is the source implementation of String's repalceAll.

Public String repalceAll (String expr,String substitute) {return Pattern.compile (expr) .matcher (this) .replaceAll (substitute);}

You can notice that the parameter of Pattern.compile (expr) is a regular expression. Therefore:

Error writing (running error report):

String tmp = Pattern.compile ("\\") .matcher ("h\") .replaceAll ("ello")

Write it correctly:

String tmp = Pattern.compile ("\") .matcher ("h\") .replaceAll ("ello"); / / output to hello

This is the end of the content of "what is the replacement of the escape character backslash in Java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report