问题
😁 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