Rahul
Rahul

Reputation: 106

Is there anyway to convert emoji to text in Java?

๐Ÿ˜ 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?

Upvotes: 2

Views: 14796

Answers (2)

Chaitanya
Chaitanya

Reputation: 2444

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:.

Upvotes: 11

paxdiablo
paxdiablo

Reputation: 881113

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.

Upvotes: 1

Related Questions