Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Output UTF-8 to Real Characters?

Tags:

java

utf-8

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?

like image 266
user595234 Avatar asked Jul 07 '26 20:07

user595234


1 Answers

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.

like image 180
Ciprian Avatar answered Jul 09 '26 08:07

Ciprian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!