如何在android中将字符串转换为unicode

时间:2021-03-23 20:13:45

I am parsing some unicodes from json to my android app, the api gives unicodes of icons like \ue600, When I add this unicode directly into the textview like textview.setText("\ue600"); it gives right icon on the textview. but when i parse this unicode from json api & then I setText that unicode it just displays \ue600 on textview. How I parse or convert these strings into unicodes to get the icons in textview.

我正在从json解析一些unicodes到我的android应用程序,api给出像\ ue600这样的图标的unicodes,当我将这个unicode直接添加到textview中时,如textview.setText(“\ ue600”);它在textview上给出了右图标。但是当我从json api解析这个unicode然后我将setText解析为unicode时它只在textview上显示\ ue600。我如何解析或将这些字符串转换为unicodes以获取textview中的图标。

Thanks

5 个解决方案

#1


6  

Convert your unicode into this format  & then use like this in your textview textview.setText(Html.fromHtml(your_unicode_here)); It should work.

将你的unicode转换为这种格式然后在textview textview.setText(Html.fromHtml(your_unicode_here))中使用这样的;它应该工作。

#2


2  

StringEscapeUtils does most of the work, however it only goes up to HTML4. For characters not covered you can make your own class and add as needed. Here is a sample class

StringEscapeUtils完成了大部分工作,但它只能达到HTML4。对于未涵盖的角色,您可以创建自己的类并根据需要添加。这是一个示例类

public class HTMLDecoder {
    public static String decodeHTML(String html) {
        String out = StringEscapeUtils.unescapeHtml4(html);
        out = out.replaceAll("®", "®");
        out = out.replaceAll("â\u0084¢", "™");
        return out;
    }
}

Add to build.bradle

添加到build.bradle

compile 'org.apache.commons:commons-lang3:3.0'

#3


1  

Following works fine for me.

以下对我来说很好。

String unicode = "\u0048\u0065\u006C\u006C\u006F";
String Title = StringEscapeUtils.unescapeJava(unicode);
System.out.println(Title);

and add dependency : compile 'commons-lang:commons-lang:2.6' in your build.gradle file.

并添加依赖:在build.gradle文件中编译'commons-lang:commons-lang:2.6'。

#4


0  

How do you parse JSON data? If you are using Gson, you can use GsonBuilder.disableHtmlEscaping() method in order to display proper characters instead of their code.

你如何解析JSON数据?如果您使用的是Gson,则可以使用GsonBuilder.disableHtmlEscaping()方法来显示正确的字符而不是代码。

#5


0  

textview.setText(Html.fromHtml(UNICODE_CHARS));

#1


6  

Convert your unicode into this format  & then use like this in your textview textview.setText(Html.fromHtml(your_unicode_here)); It should work.

将你的unicode转换为这种格式然后在textview textview.setText(Html.fromHtml(your_unicode_here))中使用这样的;它应该工作。

#2


2  

StringEscapeUtils does most of the work, however it only goes up to HTML4. For characters not covered you can make your own class and add as needed. Here is a sample class

StringEscapeUtils完成了大部分工作,但它只能达到HTML4。对于未涵盖的角色,您可以创建自己的类并根据需要添加。这是一个示例类

public class HTMLDecoder {
    public static String decodeHTML(String html) {
        String out = StringEscapeUtils.unescapeHtml4(html);
        out = out.replaceAll("®", "®");
        out = out.replaceAll("â\u0084¢", "™");
        return out;
    }
}

Add to build.bradle

添加到build.bradle

compile 'org.apache.commons:commons-lang3:3.0'

#3


1  

Following works fine for me.

以下对我来说很好。

String unicode = "\u0048\u0065\u006C\u006C\u006F";
String Title = StringEscapeUtils.unescapeJava(unicode);
System.out.println(Title);

and add dependency : compile 'commons-lang:commons-lang:2.6' in your build.gradle file.

并添加依赖:在build.gradle文件中编译'commons-lang:commons-lang:2.6'。

#4


0  

How do you parse JSON data? If you are using Gson, you can use GsonBuilder.disableHtmlEscaping() method in order to display proper characters instead of their code.

你如何解析JSON数据?如果您使用的是Gson,则可以使用GsonBuilder.disableHtmlEscaping()方法来显示正确的字符而不是代码。

#5


0  

textview.setText(Html.fromHtml(UNICODE_CHARS));