How to ignore escape characters in a string in Java--reprint

Source: Internet
Author: User

Original address: http://my.oschina.net/u/1010578/blog/366252

Cause

These days I need to work with another colleague to tune the rest interface, my side is Java he is PHP, the return message is in JSON format. After the interface call succeeds, the output returns a message with a Unicode character similar to "\u79fb\u52a8\u4e92\u8054\u7f51\u5e94\u7528", a little puzzled, remembering that Java is automatically converting Unicode characters to something pairs.

By debugging a breakpoint, it is found that the returned message is converted to "\u79fb\u52a8\u4e92\u8054\u7f51\u5e94\u7528" in the program, and two backslashes denote the character ' \ ', so the output is not Unicode corresponding to Chinese, but " Like the Unicode "string.

Solution Solutions

As long as the "\" in the received message is replaced with "\", I would like to be able to correctly output Unicode into Chinese, the first thought is to use the string ReplaceAll () method. Use ReplaceAll ("\ \", "\"), but found no change in the output result.
Check the API documentation, the ReplaceAll () method is defined as:

public String replaceAll( String regex,String replacement)  ;

That is, the first parameter refers to a regular expression, so "\ \" In the way of regular expressions, matching the string of two \ characters, rather than the ' \ ' escape character in Java. In other words, the source string that the regex parameter looks for as a regular expression is a "\u79fb\u52a8\u4e92\u8054\u7f51\u5e94\u7528" that has escaped, rather than "\\u79fb\\u52a8\\u4e92\" before escaping. \u8054\\u7f51\\u5e94\\u7528 ", so replaceall (" \ \ "," \ ") naturally has no effect.

Later on the StackOverflow found a ignore escape of the tool class, Org.apache.commons.lang.StringEscapeUtils, there is a way to ignore the escape symbols of various languages, both easy to understand, it is used directly.
Where the Unescapejava (string s) method is to handle the Java escape character, you can convert "\" in the string to "\", "'" to "'", and so on. This method of processing the above string, just to meet my needs.

public class Test () {public static void main (String[] args) {String s =" \\u79fb\\u52a8\\u4e92\\ U8054\\u7f51\\u5e94\\u7528 "; String s2 = Stringescapeutils.unescapejava (s); System.out.println (s); SYSTEM.OUT.PRINTLN (S2); }} output: \u79fb\u52a8\u4e92\u8054 \u7f51\u5e94\u7528 mobile Internet applications  

Attach the Unescapejava () method to handle the associated source code of the escaped character for easy comprehension.

PublicStaticvoid Unescapejava (Writer out, String str)Throws IOException {if (out = =NULL) {ThrowNew IllegalArgumentException ("The Writer must not being null"); }Elseif (str! =NULL) {int sz = Str.length (); Strbuilder Unicode =New Strbuilder (4);Boolean Hadslash =FalseBoolean inunicode =FalseForint i =0; I < sz; ++i) {char ch = str.charat (i);if (inunicode) {Unicode.Append (CH);if (unicode.length () = =4) {try {int NFE = Integer.parseint (unicode.tostring (),16); Out.Write ((char) NFE); Unicode.setlength (0); Inunicode =False Hadslash =False }catch (NumberFormatException var9) {ThrowNew Nestableruntimeexception ("Unable to parse Unicode value:" + Unicode, VAR9);}}}Elseif (hadslash) {Hadslash =FalseSwitch (CH) {Case' \ ': Out.Write34);BreakCase' \ ': Out.Write39);BreakCase' \ \ ': Out.Write92);BreakCase' B ': out.Write8);BreakCase' F ': out.Write12);BreakCase' n ': out.Write10);BreakCase' R ': out.write (13); break; case  ' t ': Out. Write (9); break; case  ' u ': inunicode = true; break; default:out. write (CH);} } else if (ch = = 92) {HadSlash = true; } else {out. Write (CH); }} if (hadslash) {out. Write (92);}}            

How to ignore escape characters in a string in Java--reprint

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.