Java - Evaluate String from String

2019-06-13 17:17发布

in my Java code I have this snippet:

String str = "\\u9601";

But I want it to be:

String str = "\u9601";

which represents a wide character.

Are there ways to do this?

Please help. Thanks in advance.

Supplementary:

Sorry for the badly-described question.

System.out.print("\u9601"); //this will display a Chinese character

I am currently request a webpage(URL) which response with a JSON. If dumped to Console using "System.out.print", the JSON will turn out to be 6 visible characters \, u, 9, 6, 0 and 1,but not a Chinese character in eclipse.

So actually what I want is are there APIs can convert "\\u9601" to "\u9601", since I can not hard code the Java source for the contents comes from website.

标签: java string eval
1条回答
ら.Afraid
2楼-- · 2019-06-13 17:59

If you want it to be String str = "\u9601";, keep it that way!

Edit based on the updated question

The StringEscapeUtils.unescapeJava method in the Apache Commons Lang API should be of help:

    String str = "\\u9601";
    str = StringEscapeUtils.unescapeJava(str);
    System.out.println(str);
查看更多
登录 后发表回答