Is there anyway to convert emoji to text in Java?

被刻印的时光 ゝ 提交于 2019-12-11 12:53:36

问题


😁 how can I convert emojis like this to text? I mean to convert a happy face to the words "happy" and so on. Using Java, how can I achieve this?


回答1:


You may use emoji4j library.

String text = "A 🐱, 🐶 and a 🐭 became friends❤️. For 🐶's birthday party, they all had 🍔s, 🍟s, 🍪s and 🍰.";

EmojiUtils.shortCodify(text); //returns A :cat:, :dog: and a :mouse: became friends:heart:. For :dog:'s birthday party, they all had :hamburger:s, :fries:s, :cookie:s and :cake:.



回答2:


Since that emoji is simply a standard Unicode code point (U+1F601, see here), probably the best way is to set up a map which translates them into strings.

By way of example, here's a piece of code that creates a string-to-string map to allow you to look up and translate that exact code point:

import java.util.HashMap;
import java.util.Map;
class Xyzzy {
    public static Map<String,String> xlat = new HashMap<String, String>();
    public static void main (String[] args) {
        xlat.put("\uD83D\uDE01", "happy");
        System.out.println(xlat.get("\uD83D\uDE01"));
    }
}

You can add as many code points to the map as you wish, and use Map.get() to extract the equivalent text for any of them.



来源:https://stackoverflow.com/questions/34802721/is-there-anyway-to-convert-emoji-to-text-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!