0

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.

9
  • 2
    You could delete one of the backslashes. Commented Nov 27, 2010 at 3:49
  • No, it doesn’t represent a “wide character”. It represents an abtract Unicode code point, U+9601. It is no wider than a LATIN CAPITAL LETTER A. Abstract code points have no width. Commented Nov 27, 2010 at 3:49
  • I must be missing something, because if you wanted to use a unicode character escape you could delete a '\' from str. Commented Nov 27, 2010 at 3:51
  • Well, that, or just replace "\\u" with "\u". Commented Nov 27, 2010 at 3:54
  • @tchrist: I think he meant "wide" as in "double-byte". (Obviously, all string characters in Java are two bytes, so I think he really meant "non-ASCII.") Commented Nov 27, 2010 at 3:55

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I can hard code the source, what I supplied is just an sample.
Indeed, the contents of the String variable is read from a website.
Hi have the same problem but for ASCII characters, String str = "\\026" to String str = "\026"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.