Java 中是否有将表情符号转换为文本的方法?
Is there anyway to convert emoji to text in Java?
如何将这样的表情符号转换为文本?我的意思是将笑脸转换为 "happy" 等字样。使用 Java,我该如何实现?
由于表情符号只是一个标准的 Unicode 代码点(U+1F601
,参见 here),最好的方法可能是设置一个将它们转换为字符串的映射。
例如,这里有一段代码创建了一个 string-to-string 地图,让您可以查找和翻译确切的代码点:
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"));
}
}
您可以根据需要向地图添加任意数量的代码点,并使用 Map.get()
为其中任何一个提取等效文本。
您可以使用 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:.
如何将这样的表情符号转换为文本?我的意思是将笑脸转换为 "happy" 等字样。使用 Java,我该如何实现?
由于表情符号只是一个标准的 Unicode 代码点(U+1F601
,参见 here),最好的方法可能是设置一个将它们转换为字符串的映射。
例如,这里有一段代码创建了一个 string-to-string 地图,让您可以查找和翻译确切的代码点:
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"));
}
}
您可以根据需要向地图添加任意数量的代码点,并使用 Map.get()
为其中任何一个提取等效文本。
您可以使用 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:.