3

In Java, how can I output UTF-8 to real string?

     我们

     \u6211\u4eec

        String str = new String("\u6211\u4eec"); 
       System.out.println(str); // still ouput \u6211\u4eec, but I expect 我们 to be an output

-----
String tmp = request.getParameter("tag");
        System.out.println("request:"+tmp);
        System.out.println("character set :"+request.getCharacterEncoding());       
        String tmp1 = new String("\u6211\u4eec");       
        System.out.println("string equal:"+(tmp.equalsIgnoreCase(tmp1)));
        String tag = new String(tmp);       
        System.out.println(tag);

request:\u6211\u4eec
character set :UTF-8
string equal:false
\u6211\u4eec

From the output, the value from the request is the same as the string value of tmp1, but why does equalsIgnoreCase output false?

Grant Miller
  • 24,187
  • 16
  • 134
  • 150
user595234
  • 5,829
  • 24
  • 75
  • 100
  • possible duplicate of [How to convert Strings to and from UTF8 byte arrays in Java](http://stackoverflow.com/questions/88838/how-to-convert-strings-to-and-from-utf8-byte-arrays-in-java) – hrv Dec 07 '13 at 21:13

2 Answers2

1

Java String are encoded in UTF-16. I do not see any problem in your code, I would believe the problem comes from your console and it doesn't show correctly the content of the String.

If you are using eclipse, change your console encoding here to UTF-8

Eclipse > Preferences > General > Workspace > Text file encoding
Henri Lapierre
  • 1,992
  • 4
  • 17
  • 19
1

did you try to display just one of them? like

   String str = new String("\u6211"); 
   System.out.println(str);

I bet there is a problem in how you create that string.

Ciprian
  • 2,829
  • 3
  • 26
  • 28