2种方式实现:
注意:如果发现运行时java.lang.NoClassDefFoundError:异常就是缺少了jar包。添加对应的jar包就可以。
一、emoji-java-4.0.0.jar实现
<!--处理emoji表情--> <dependency> <groupId>com.vdurmont</groupId> <artifactId>emoji-java</artifactId> <version>4.0.0</version> </dependency> <!-- emoji表情依赖类--> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> 代码:
public static void main(String args[]){ String str = "苍天厚土\uD83D\uDE01 \uD83D\uDC36 \uD83E\uDD14 \uD83D\uDC7B \uD83D\uDE92"; System.out.println("原始字符为:\n" + str); System.out.println("to aliases 之后:"); System.out.println(EmojiParser.parseToAliases(str)); System.out.println(EmojiParser.parseToAliases(str, EmojiParser.FitzpatrickAction.PARSE)); System.out.println(EmojiParser.parseToAliases(str, EmojiParser.FitzpatrickAction.REMOVE)); System.out.println(EmojiParser.parseToAliases(str, EmojiParser.FitzpatrickAction.IGNORE)); System.out.println(EmojiParser.parseToAliases(str, EmojiParser.FitzpatrickAction.IGNORE)); System.out.println(EmojiParser.parseToUnicode(str)); }
运行结果:
原始字符为:
苍天厚土😁 🐶 🤔 👻 🚒
to aliases 之后:
苍天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
苍天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
苍天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
苍天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
苍天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
苍天厚土😁 🐶 🤔 👻 🚒
二、java-emoji-converter-0.1.1.jar方式
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> </dependency> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>java-emoji-converter</artifactId> <version>0.1.1</version> 代码:
public static void main(String args[]){ String str = "苍天厚土\uD83D\uDE01 \uD83D\uDC36 \uD83E\uDD14 \uD83D\uDC7B \uD83D\uDE92"; EmojiConverter emojiConverter = EmojiConverter.getInstance(); System.out.println("原始字符为:\n" + str); System.out.println("to aliases 之后:"); System.out.println(emojiConverter.toAlias(str)); System.out.println(emojiConverter.toUnicode(str)); }
运行结果:
原始字符为:
苍天厚土😁 🐶 🤔 👻 🚒
to aliases 之后:
苍天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
苍天厚土😁 🐶 🤔 👻 🚒
备注:
经过测试,神奇的事情发生了,使用方法一加密,方法二可以正常解密。使用方法二加密,方法一可以正常解密。应该是2个jar包底层数据一致吧。
将一些逻辑问题使用代码实现
来源:oschina
链接:https://my.oschina.net/u/4000302/blog/4328890