String name = ...; //some method
System.out.println(name); //output \u5f20\u4f73\u73ae
String name2 = "\u5f20\u4f73\u73ae"; //looks the same as the output above
System.out.println(name2); \\output 张佳玮
I'm wondering why name
and name2
output different strings and how to make name
has the same output as name2
.
If System.out.println(name)
prints out a backslash followed by a u
etc, then that's what's in the string... whereas in
String name2 = "\u5f20\u4f73\u73ae";
... you've got Unicode escape sequences... that's just 3 characters. If you want backslashes, you need to escape them:
String name2 = "\\u5f20\\u4f73\\u73ae";
See JLS 3.3 for more about Unicode escape sequences (which can come anywhere in the source) and JLS 3.10.6 for more about string and character literal escape sequences.
To go the other way - if you have a string value that starts with \
then u
etc, you'll have to parse it. I don't know of anything in the regular Java libraries to do this (other than going via Properties
, which would be very bizarre), but I'm sure there are third-party libraries which would.
However, you should look at where the data has come from and whether you should really be parsing it yourself - normally text with Unicode escape sequences comes as part of JSON or something similar, in which case you should use a JSON parser.
See more on this question at Stackoverflow