How to ignore escape characters in strings in Java — reprint

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

cause

In the past few days, I need to debug the rest interface with another colleague. My side is Java, he is PHP, and the return message is in JSON format. After the interface call is successful, there are Unicode characters like "\ u79fb \ u52a8 \ u4e92 \ u8054 \ u7f51 \ u5e94 \ u7528" in the returned message. I'm a little puzzled. I remember that Java will automatically convert Unicode characters into Chinese.

Through the breakpoint debug, it is found that the returned message is converted into "\ u79fb \ u52a8 \ u4e92 \ u8054 \ u7f51 \ u5e94 \ u7528" in the program. The two backslashes represent the character '\', so the output is not the Chinese corresponding to Unicode, but the string "like Unicode".

Solution

As long as the "\" in the obtained message is replaced with "\", I think I can normally output Unicode into Chinese. The first thought is to use the replaceall() method of string. Replaceall ("\ \", "\") is used, but no change is found in the output result. After checking the API document, the definition of replaceall() method is:

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

Later, I found a tool class on stackoverflow that ignores escape, org apache. commons. Lang. stringescapeutils, which has the method of ignoring escape symbols of various languages, is easy to use and easy to understand, so it can be used directly. The unescapejava (string s) method is used to process Java escape characters, which can convert "\" in the string to "\", "'" to "'", etc. Processing the above strings in this way can just meet my needs.

public static void main(String<span class="hljs-special"&gt;[<span class="hljs-special"&gt;] args) <span class="hljs-special"&gt;{
    String s = "<span class="hljs-command"&gt;\\u79fb<span class="hljs-command"&gt;\\u52a8<span class="hljs-command"&gt;\\u4e92<span class="hljs-command"&gt;\\u8054<span class="hljs-command"&gt;\\u7f51<span class="hljs-command"&gt;\\u5e94<span class="hljs-command"&gt;\\u7528";
    String s2 = StringEscapeUtils.unescapeJava(s);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(s);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(s2);

<span class="hljs-special"&gt;}

Output result: < span class = "hljs command" > \ u79fb < span class = "hljs command" > \ u52a8 < span class = "hljs command" > \ u4e92 < span class = "hljs command" > \ u8054 < span class = "hljs command" > \ u7f51 < span class = "hljs command" > \ u5e94 < span class = "hljs command" > \ u7528 mobile Internet application

Attach the relevant source code of unescapejava () method to process escape characters for easy understanding.


   
        <span class="hljs-keyword"&gt;for(<span class="hljs-keyword"&gt;int i = <span class="hljs-number"&gt;0; i < sz; ++i) {
        <span class="hljs-keyword"&gt;char ch = str.charAt(i);
        <span class="hljs-keyword"&gt;if(inUnicode) {
            unicode.<span class="hljs-keyword"&gt;append(ch);
            <span class="hljs-keyword"&gt;if(unicode.length() == <span class="hljs-number"&gt;4) {
                <span class="hljs-keyword"&gt;try {
                    <span class="hljs-keyword"&gt;int nfe = Integer.parseInt(unicode.toString(),<span class="hljs-number"&gt;16);
                    out.<span class="hljs-keyword"&gt;write((<span class="hljs-keyword"&gt;char)nfe);
                    unicode.setLength(<span class="hljs-number"&gt;0);
                    inUnicode = <span class="hljs-keyword"&gt;false;
                    hadSlash = <span class="hljs-keyword"&gt;false;
                } <span class="hljs-keyword"&gt;catch (NumberFormatException var9) {
                    <span class="hljs-keyword"&gt;throw <span class="hljs-keyword"&gt;new NestableRuntimeException(<span class="hljs-string"&gt;"Unable to parse unicode value: " + unicode,var9);
                }
            }
        } <span class="hljs-keyword"&gt;else <span class="hljs-keyword"&gt;if(hadSlash) {
            hadSlash = <span class="hljs-keyword"&gt;false;
            <span class="hljs-keyword"&gt;switch(ch) {
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'\"':
                    out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;34);
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'\'':
                    out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;39);
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'\\':
                    out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;92);
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'b':
                    out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;8);
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'f':
                    out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;12);
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'n':
                    out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;10);
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'r':
                    out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;13);
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'t':
                    out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;9);
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;case <span class="hljs-string"&gt;'u':
                    inUnicode = <span class="hljs-keyword"&gt;true;
                    <span class="hljs-keyword"&gt;break;
                <span class="hljs-keyword"&gt;default:
                    out.<span class="hljs-keyword"&gt;write(ch);
            }
        } <span class="hljs-keyword"&gt;else <span class="hljs-keyword"&gt;if(ch == <span class="hljs-number"&gt;92) {
            hadSlash = <span class="hljs-keyword"&gt;true;
        } <span class="hljs-keyword"&gt;else {
            out.<span class="hljs-keyword"&gt;write(ch);
        }
    }

    <span class="hljs-keyword"&gt;if(hadSlash) {
        out.<span class="hljs-keyword"&gt;write(<span class="hljs-number"&gt;92);
    }

}
   

}



总结

以上是编程之家为你收集整理的java中如何忽略字符串中的转义字符--转载全部内容,希望文章能够帮你解决java中如何忽略字符串中的转义字符--转载所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

编程之家官方1群

编程之家官方2群



The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>