java 无论如何在Java中将表情符号转换为文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34802721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there anyway to convert emoji to text in Java?
提问by Rahul
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?
如何将这样的表情符号转换为文本?我的意思是将一张快乐的脸转换成“快乐”等词。使用 Java,我怎样才能做到这一点?
回答by Chaitanya
You may use emoji4jlibrary.
您可以使用emoji4j库。
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:.
回答by paxdiablo
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.
由于该表情符号只是一个标准的 Unicode 代码点(U+1F601
,请参见此处),因此最好的方法可能是设置一个将它们转换为字符串的映射。
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.
您可以根据需要向地图添加任意数量的代码点,并用于Map.get()
提取其中任何一个的等效文本。