无法显示特殊字符 [英] Not able to display special characters

查看:2121
本文介绍了无法显示特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在屏幕上显示特殊字符(抛光字符)。我有一个要求,我从数据库中获取具有一些特殊字符的数据。我以xml格式获取数据(xml没有将其识别为字符串)并将其传递给我尝试显示数据的操作。我试图将特殊字符的Uniciode设为ł 但是当我尝试显示时,这会转换为& amp ;#X142; 因此我无法显示它,因为它不会将其作为字符串。

I am unable to display special characters (polish characters) on screen. I have a requirement where I get the data from database which has some special characters. I get the data in an xml format (The xml is not recognizing it as a string) and pass it to action where I try to display the data. I am trying to get the Uniciode of the special character as ł but when I try to display, this gets converted to ł and so I am unable to display it because it does not take it as a string.

String ex1="ł";
System.out.println("ex1...."+ex1);
output:: ?

我试图使用以下代码获取Unicode ::

I am trying to get the Unicode using the following code::

    public static String convert (String str) throws UnsupportedEncodingException
    {
        String tc = str;
        String output = "";
        char[] ca = tc.toCharArray();
        for (int i = 0; i < ca.length; ++i) 
             {
               char a = ca[i];
               if ((int) a > 255) 
                    {
            output += "&"+"#X"+ Integer.toHexString((int) a) + ";";
               } 
                   else 
                   {
            output += a;
              }
        }
        return output;
    }

输出为:如果输入为 str =ł然后输出=&#X142;

The output is: If input is given as str="ł" then output=&#X142;

推荐答案

不要重新发明轮子!使用 StringEscapeUtils <的> escapeXml 方法来自 Apache Commons Lang 库的/ a>类,它提供了这个简单的解决方案:

Don't reinvent the wheel! Use the escapeXml method of the StringEscapeUtils class from the Apache Commons Lang library, which makes for this simple solution:

StringEscapeUtils.escapeXml(input);

这篇关于无法显示特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆